3

我具有基于 AOP 的登录功能,具有以下设置

上下文xml配置:

<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 调用本身?我可以很容易地禁用日志记录,但我可以禁用/启用整个后台进程和调用吗?

如果需要任何澄清,请告知。

4

2 回答 2

3

由于您使用的是 Spring AOP,因此启用或禁用方面的一种快速方法可能是简单地使用 Bean 配置文件。

定义配置文件说enableAOP:将aop:config包装在特定配置文件下的配置文件中

<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配置文件即可。

于 2012-07-26T17:44:13.480 回答
2

自从提出这个问题以来已经有一段时间了,但这是我为 Spring 2 提出的:

创建一个aop-context-off.xml带有空<beans>声明的空 xml 文件 ( )。aop-context-enabled.xml然后使用您的 AOP 声明创建例如一个文件。

最后,在导入 XML 时,您可以使用:

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

这将寻找一个名为 AOP_CONTEXT_SUFFIX 的系统变量,如果没有找到,则将值强制为off.

所以在上面的例子中,通过设置AOP_CONTEXT_SUFFIXenabled会加载aop-context-enabled.xml

因此开关是系统变量,您可以在服务器启动时轻松启用/禁用它。

于 2013-07-29T13:33:20.180 回答