Configuration for Selective Logging
Listing 1 shows a good configuration for weaving logging into each and every method call (that is defined in the bean interface) in the targeted bean. What if you wanted to trace selective method calls? This scenario is more appropriate when crosscutting concerns are applied selectively, like security, transaction, audit-trail, etc.
Listing 3 shows the configuration for selective logging:
Listing 3. XML Configuration File for Selective Logging
<bean id="maintenanceServiceBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<value> theLogger </value>
<bean id="theLogger" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<ref local=" loggingInterceptor "/>
<bean id=" maintenanceServiceImpl " class="MaintenanceService">
<bean id=" loggingInterceptor" class="LoggingInterceptor"/>
The difference between the configurations in Listing 3 and Listing 1 is that in Listing 3 the logging interceptor (theLogger) points to a Spring class called RegexpMethodPointcutAdvisor instead of pointing to its implemented class, as was the case in Listing 1. This means that theLogger is a class for regexp method pointcuts that holds an advice (in this case, loggingInterceptor, which implements the actual logging mechanism as an advice), and it can be configured using "pattern" and "patterns" pass-through properties. These properties actually allow the advice to be applied on a selected public method (or methods) or on each public method of the intercepted bean.
The "pattern" property takes one pattern of a method name, like .*getList.*. The pattern should be a fully qualified method name. If * is supplied, then the advice applies on all the public methods of the intercepted bean. The "patterns" property is actually an array of patterns (e.g., .*getList.*,.*getCounts.*). The patterns should be comma-separated.
A Powerful and Flexible Concept
Implementing logging as an aspect with Spring's AOP framework is a powerful and flexible concept. Since Spring is lightweight, the logging implementation is simply a POJOnot tightly coupled with any framework. This makes it reusable in any system whether container-based or another lightweight framework.
Along the same lines as logging, you can implement other crosscutting concerns. For example, suppose you need to implement an audit trail such that an audit trail component is invoked whenever there is a call to the database. For this simple purpose, the audit trail service just needs to implement the MethodBeforeAdvice interface of Spring and provide the necessary implementation for the
before () method.