2013-08-23 30 views
1

在我的公司,我們正在研究面向方面的跟蹤攔截器,類似於DebugInterceptor。我們正在配置一個CustomizableTraceInterceptor並使用BeanNameAutoProxyCreator爲AOP自動代理bean。BeanNameAutoProxyCreator和導入配置之間的隱式依賴關係

我們面臨的問題是,當我們在配置介紹BeanNameAutoProxyCreator

 
@Configuration 
@Import(BConfig.class) 
@EnableAspectJAutoProxy 
public class AConfig { 
    @Bean 
    public static BeanNameAutoProxyCreator beanNameAutoProxyCreator() { 
     BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator(); 
     beanNameAutoProxyCreator.setInterceptorNames(new String[] {DEBUG_INTERCEPTOR_NAME}); 
     beanNameAutoProxyCreator.setBeanNames(new String[] {BEANS_NAMES_EXPRESSION}); 
     return beanNameAutoProxyCreator; 
    } 
} 

我們得到一個org.springframework.beans.factory.NoSuchBeanDefinitionException:無型的排位豆[X] ,其中X是Resteasy代理。此Resteasy代理在BConfig中聲明。

現在,如果我將Resteasy Proxy Bean配置移至AConfig,此問題就解決了,@DependsOn也解決了這個問題。

我的問題是3:何時Spring能夠解決bean之間的依賴關係?爲什麼使用BeanNameAutoProxyCreator改變這種行爲?什麼是解決這個問題的推薦方法(BeanPostProcessor,@DependsOn等)。

回答

1

靜態BeanNameAutoProxyCreator依賴於正常的bean(可能是由於BEANS_NAMES_EXPRESSION)。因爲它是靜態的,所以在任何其他bean之前加載/ bootstrapped,特別是在豆處理之前@Import。所以基本上在分析要處理的豆時,BConfig尚未加載。這就是爲什麼當你將bean移動到AConfig或者依賴於這個bean的時候它是有效的。

我可能會恢復使用BeanNameAutoProxyCreator,並依靠@EnableAspectJAutoProxy連同使用bean pointcut附加所需攔截器的方面。

@EnableAspectJAutoProxy旁邊引入BeanNameAutoProxyCreator還有另一個風險,由於2種不同的AOP策略/機制,它可能導致創建代理的代理。