Jobin Abraham
Jobin Abraham

Reputation: 51

Can we disable AOP invocations?

I have AOP based loggin functionality with the following settings

Context xml configuration:

<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 properties:

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

Is there any way that I can disable the AOP invocations itself depending upon the enviroment? I can disable the logging very easily but can I disable/enable the entire background process and invocations?

Please let if any clarifications are required.

Upvotes: 3

Views: 4793

Answers (2)

Michael Laffargue
Michael Laffargue

Reputation: 10314

it's been a while since this question was asked, but here is what I came up with for Spring 2:

Create an empty xml file (aop-context-off.xml) with an empty <beans> declaration. Then create for example an aop-context-enabled.xml file with your AOP declaration.

Finally when importing the XML you could use :

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

This will look for a system variable called AOP_CONTEXT_SUFFIX and if not found force the value to off.

So in the example above, by setting AOP_CONTEXT_SUFFIX to enabled it will load the aop-context-enabled.xml

So the switch being the system variable, you can easily Enable/Disable it at server start.

Upvotes: 2

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

Since you are using Spring AOP, one quick way to enable or disable aspects could be to simply use Bean profiles.

Define a profile say enableAOP: Wrap the aop:config in say a configuration file under the specific profile

<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>

Now, whichever environment you want the specific aspects enabled, just run with enableAOP profile on.

Upvotes: 3

Related Questions