2012-07-26 106 views
3

我有以下設置我們可以禁用AOP調用嗎?

上下文XML配置AOP基於洛功能:

<bean id="performanceMonitor" 
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" /> 

<aop:config> 
     <aop:pointcut id="allServiceMethods" 
      expression="execution(* com.eshop.sfweb.service.impl..*(..))" /> 
    <aop:pointcut id="allEpServices" 
      expression="execution(* com.service.catalog..*(..))" /> 
     <aop:advisor pointcut-ref="allServiceMethods" 
      advice-ref="performanceMonitor" order="2" /> 
     <aop:advisor pointcut-ref="allEpServices" 
      advice-ref="performanceMonitor" order="2" /> 
    </aop:config> 

Log4j屬性:

log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorIntercept 
or=${ep.perflog.level},PERFORMANCE 
log4j.appender.PERFORMANCE.File=webAppRoot:WEB-INF/log/performance.log 
log4j.appender.PERFORMANCE.threshold=DEBUG 
log4j.appender.PERFORMANCE=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.PERFORMANCE.DatePattern='.'yyyy-MM-dd 
log4j.appender.PERFORMANCE.layout=org.apache.log4j.PatternLayout 
log4j.appender.PERFORMANCE.layout.ConversionPattern=%d -- %-5p [%t | %F:%L] 
-- %m%n 

有沒有辦法,我可以禁用本身取決於AOP調用任何方式在環境? 我可以非常容易地禁用日誌記錄,但是可以禁用/啓用整個後臺進程和調用嗎?

如果需要澄清,請留言。

回答

3

由於您使用的是Spring AOP,因此啓用或禁用方面的一種快速方法可能是簡單地使用Bean配置文件。

定義配置文件說enableAOP: 包裹AOP:配置中說配置文件中的特定配置

<beans profile="enableAOP"> 
    <aop:config> <aop:pointcut id="allServiceMethods" 
    expression="execution(* com.eshop.sfweb.service.impl..*(..))" /> 
    <aop:pointcut id="allEpServices" expression="execution(* 
    com.service.catalog..*(..))" /> 
.... 
</beans> 

現在,無論環境中,你要啓用特定方面下,只是與enableAOP譜運行。

+0

我可以知道哪個版本的spring支持bean的這個配置文件屬性?我在Spring 2.0中嘗試過,但未能解析spring bean xml。 – sundar 2013-04-24 05:34:34

+0

對於那些想知道的,配置文件已被添加到春季3. – 2013-07-25 15:50:54

+0

優秀的答案。簡潔,優雅和精確。 – Bevilaqua 2017-04-24 00:32:35

2

它已經有一段時間,因爲這個問題被問,但這裏是我想出了春2:

與空<beans>聲明創建一個空的XML文件(aop-context-off.xml)。 然後用您的AOP聲明創建例如aop-context-enabled.xml文件。

最後導入XML時,你可以使用:

<import resource="aop-context-${AOP_CONTEXT_SUFFIX:off}.xml" /> 

這將查找名爲AOP_CONTEXT_SUFFIX系統變量,如果沒有發現力值off

因此,在上面的例子中,通過設定AOP_CONTEXT_SUFFIXenabled它會加載aop-context-enabled.xml

所以開關作爲系統變量,可以很容易地啓用/在服務器啓動禁用它。

相關問題