You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring AOP配置遇UnsatisfiedDependencyException问题求助

问题定位与解决

核心矛盾点

  1. 重复实例化冲突:LoggingAspect同时标注了@Component(Spring自动扫描创建)和在AspectConfiguration中通过@Bean手动声明,导致Spring处理依赖逻辑混乱。
  2. ObjectMapper未注册为Spring Bean:容器中不存在com.fasterxml.jackson.databind.ObjectMapper类型的Bean,无法完成依赖注入。

具体修复步骤

1. 统一切面实例化方式

二选一,不要同时用两种方式创建LoggingAspect:

  • 方式一:保留@Component注解
    去掉AspectConfiguration中声明LoggingAspect的@Bean方法,确保Spring组件扫描范围覆盖LoggingAspect所在包(通过@ComponentScan或XML的<context:component-scan>配置)。
  • 方式二:保留@Bean声明
    删除LoggingAspect上的@Component注解,在配置类的@Bean方法中手动注入依赖:
@Configuration
@EnableAspectJAutoProxy
public class AspectConfiguration {
    @Bean
    public LoggingAspect loggingAspect(ObjectMapper objectMapper, JobExecutionService jobExecutionService) {
        return new LoggingAspect(objectMapper, jobExecutionService);
    }
}

2. 注册ObjectMapper为Spring Bean

在Spring配置类中添加ObjectMapper的Bean声明,比如可以直接加在AspectConfiguration里:

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper();
}

如果需要自定义Jackson配置,可在该方法中添加对应设置(如日期格式化、序列化规则等)。

3. 确认依赖完整性

检查pom.xml是否添加了Jackson核心依赖(仅aspectj依赖不足以提供ObjectMapper):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.10</version> <!-- 与Spring4.3.6兼容的推荐版本 -->
</dependency>

内容的提问来源于stack exchange,提问作者denizer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 07:30:23