Home > Net >  Spring AOP: UnsatisfiedDependencyException error with configuration with annotation
Spring AOP: UnsatisfiedDependencyException error with configuration with annotation

Time:09-29

I'm having a trouble while configuring Spring AOP. I created an aspect class which is below:

@Slf4j
@Aspect
@RequiredArgsConstructor
@Component
public class LoggingAspect {

    private static final Logger logger = CommonLogger.getLogger(LoggingAspect.class);

    private final ObjectMapper mapper;
    private final JobExecutionService jobExecutionService;
}

Then I added a configuration file:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@RequiredArgsConstructor
public class AspectConfiguration {
    private final ObjectMapper objectMapper;
    private final JobExecutionService jobExecutionService;

    @Bean
    public LoggingAspect loggingAspect() {
        return new LoggingAspect(objectMapper, jobExecutionService);
    }
}

But when I started the application, I am getting below errors:

Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'aspectConfiguration' defined in URL: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I have added aspectjrt and aspectjweaver dependencies to pom.xml.

Spring version is 4.3.6

I couldn't figure out where the problem is. Any help would be appreciated.

CodePudding user response:

Add class:

@Configuration
public class BeanConfig {

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