2015-06-22 13 views
0

當前我正在使用Spring AOP面臨一個奇怪的錯誤。我簡單的目標是語域下面類作爲一個方面:在Java中配置方面時拋出'BeanCurrentlyInCreationException'

@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配置相同的結果(共同豆類)失敗:

@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.BeanCurrentlyInCr eationException:創建名爲'aopProxyInitializer'的bean時出錯:請求的bean當前正在創建:是否存在無法解析的循環引用?

你知道這是爲什麼嗎?提前致謝!

+0

糾正我,如果我錯了,不應該我方面命名爲方面而不是類,像公共類AopProxyInitializer。 –

+0

如果它適用於XML,爲什麼不使用它? –

+0

@We Borg這是使用AspectJ方面的情況。確實對我來說使用XML是可以的,我只是開始用普通的Java來做這件事,並想知道爲什麼會發生這種情況。 – pklndnst

回答

0

問題:@Pointcut(「execution(public * *(..))」)包含導致異常的SpringInstrumentationConfig類。您可以向publicMethodPointcut添加& &!target(path.to.SpringInstrumentationConfig),或將方面中的聲明從配置類移動到上下文。

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AopProxyInitializer.class); 
+0

你有一個想法,爲什麼這在XML中工作,但? – pklndnst

相關問題