目前我使用Spring AOP遇到了一个奇怪的错误。我的简单目标是将以下类重新注册为一个方面:
@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;
}
}
通过XML这样做很好:
<aop:aspectj-autoproxy expose-proxy="true"/>
<bean class="com.big.instrumentation.spring.aspect.AopProxyInitializer"/>
但尝试使用此Java配置(与我的其他bean一起)获得相同的结果会失败:
@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();
}
}
结果是以下异常: org.springframework.beans.factory.BeanCurrentlyInCreationException:使用名称&#39; aopProxyInitializer&#39;创建bean时出错:正在创建请求的bean:是否存在无法解析的循环引用?
你知道为什么会这样吗?提前谢谢!
答案 0 :(得分:0)
问题:@Pointcut(&#34; execution(public * *(..))&#34;)包含导致异常的SpringInstrumentationConfig类。您可以添加&amp;&amp; !target(path.to.SpringInstrumentationConfig)到publicMethodPointcut或将声明af方面从配置类移动到上下文。
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AopProxyInitializer.class);