Spring throwing 'BeanCurrentlyInCreationException' when configuring aspect in Java

742 views Asked by At

Currently I'm facing a strange error using Spring AOP. My simple goal is to resgister the following class as an aspect:

@Aspect
public class AopProxyInitializer {

  @Pointcut("execution(public * *(..))")
  public void publicMethodPointcut() {

  }

  @Around("publicMethodPointcut()")
  public Object showInstrumentationOutput(ProceedingJoinPoint joinPoint) {
     try {
        return joinPoint.proceed();
     } catch (Throwable throwable) {
        throwable.printStackTrace();
     }
     return null;
  }
}

Doing so via XML works fine:

<aop:aspectj-autoproxy expose-proxy="true"/>
<bean class="com.big.instrumentation.spring.aspect.AopProxyInitializer"/>

But trying to reach the same result using this Java configuration (together with my other beans) fails:

@Configuration
@EnableAspectJAutoProxy
public class SpringInstrumentationConfig {

   @Bean
   public SpringContextProvider provider() {
      return new SpringContextProvider();
   }

  @Bean
  public SpringAdvisedBeanService beanService 
  (SpringContextProvider provider) {
    return new SpringAdvisedBeanService(provider);
  }

  @Bean
  public AopProxyInitializer aopProxyInitializer()
  {
      return new AopProxyInitializer();
  }
}

The outcome is the following exception: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'aopProxyInitializer': Requested bean is currently in creation: Is there an unresolvable circular reference?

Do you have any idea why this is the case? Thanks in advance!

1

There are 1 answers

0
Matthias Danetzky On

Problem: @Pointcut("execution(public * *(..))") includes the SpringInstrumentationConfig class which causes the exception. You can either add && !target(path.to.SpringInstrumentationConfig) to the publicMethodPointcut or move the declaration af aspects from configuration class to the context.

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AopProxyInitializer.class);