RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Implement Logging as an Aspect Using Spring's AOP Framework : Page 3

Aspect-oriented programming (AOP) enables you to write code for a crosscutting functionality and then apply it declaratively to existing code. Logging is a prime candidate for implementation as an aspect, and Spring offers the AOP framework to get it done.


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">
<property name="proxyInterfaces"> <value>IMaintenanceService</value> </property> <property name="target"> <ref bean="maintenanceServiceImpl"/> </property> <property name="interceptorNames"> <list> <value> theLogger </value> </list> </property> </bean> <bean id="theLogger" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local=" loggingInterceptor "/> </property> <property name="patterns"> <value>.*getList.*,.*getCounts.*</value> </property> </bean> <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 POJO—not 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.

Gopal Sharma is a manager with Capgemini India. He has a wide range of experience with Java technologies and has assumed many project roles, from developer to architect.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date