2012-11-01 64 views
17

Spring AOP有一個名爲CustomizableTraceInterceptor的方法級示蹤器。使用Spring的XML配置方法,一會設置此示蹤劑像這樣:Spring Spring AOP:將CustomizableTraceInterceptor與JavaConfig配合使用@EnableAspectJAutoProxy,而不是XML <aop:advisor>

<bean id="customizableTraceInterceptor" class=" 
    org.springframework.aop.interceptor.CustomizableTraceInterceptor"> 
    <property name="enterMessage" value="Entering $[methodName]($[arguments])"/> 
    <property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/> 
</bean> 

<aop:config> 
    <aop:advisor advice-ref="customizableTraceInterceptor" 
    pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/> 
</aop:config> 

我想使用Spring的JavaConfig風格(即以Java註釋的優勢,特別是@EnableAspectJAutoProxy在JavaConfig激活的AspectJ)設立上述配置。

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages = { "some.package" }) 
@ComponentScan(basePackages = { "some.package2", "some.package3" }) 
@EnableAspectJAutoProxy 
public class FacebookDomainConfiguration { 

    @Bean someBean() { 
    ... 
    } 
... 
} 

什麼是@EnableAspectJAutoProxy風格的等效<aop:advisor advice-ref="customizableTraceInterceptor" ...>

回答

3

不幸的是,您不能這樣做,因爲Java語言不支持在Spring JavaConfig中支持此方法所需的方法文字。爲此打開了一個錯誤,但標記爲「不會修復」:https://jira.springsource.org/browse/SPR-8148

在bug報告中提到的兩個選項是:

  1. 通過包括使用@ImportResource
  2. 將任何現有<aop:config> elemements使用@Aspect風格相關的XML片斷繼續使用<aop:config>[這是不可能的CustomizableTraceInterceptor]
22

我做這種方式:

@Configuration 
@EnableAspectJAutoProxy(proxyTargetClass=true) 
public class TraceLoggerConfig { 

    @Bean 
    public CustomizableTraceInterceptor customizableTraceInterceptor() { 
     CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor(); 
     customizableTraceInterceptor.setUseDynamicLogger(true); 
     customizableTraceInterceptor.setEnterMessage("Entering $[methodName]($[arguments])"); 
     customizableTraceInterceptor.setExitMessage("Leaving $[methodName](), returned $[returnValue]"); 
     return customizableTraceInterceptor; 
    } 

    @Bean 
    public Advisor jpaRepositoryAdvisor() { 
     AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); 
     pointcut.setExpression("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"); 
     return new DefaultPointcutAdvisor(pointcut, customizableTraceInterceptor()); 
    } 

} 
+3

謝謝!這應該被接受爲正確的答案。 – AndiDev

+0

它適用於彈簧靴1.2.5和彈簧4.1.7。 – smartwjw

+0

我使用上面的代碼,但'CustomizableTraceInterceptor'似乎沒有觸發 –

15

只想添加到AdrienC的響應。 我將使用點式引用一個聚合點,更清晰的分離,恕我直言

package org.example; 

@Configuration 
@EnableAspectJAutoProxy 
@Aspect 
public class AopConfiguration { 
    /** Pointcut for execution of methods on {@link Service} annotation */ 
    @Pointcut("execution(public * (@org.springframework.stereotype.Service org.example..*).*(..))") 
    public void serviceAnnotation() { } 

    /** Pointcut for execution of methods on {@link Repository} annotation */ 
    @Pointcut("execution(public * (@org.springframework.stereotype.Repository org.example..*).*(..))") 
    public void repositoryAnnotation() {} 

    /** Pointcut for execution of methods on {@link JpaRepository} interfaces */ 
    @Pointcut("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))") 
    public void jpaRepository() {} 

    @Pointcut("serviceAnnotation() || repositoryAnnotation() || jpaRepository()") 
    public void performanceMonitor() {} 

    @Bean 
    public PerformanceMonitorInterceptor performanceMonitorInterceptor() { 
     return new PerformanceMonitorInterceptor(true); 
    } 

    @Bean 
    public Advisor performanceMonitorAdvisor() { 
     AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); 
     pointcut.setExpression("org.example.AopConfiguration.performanceMonitor()"); 
     return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor()); 
    } 
} 
+2

在我的愚見,這應該被標記爲正確的答案,剛剛與Spring 4驗證。:) –

+0

謝謝你們!我從早上起就在尋找這個解決方案!)) – Dante