View RSS Feed

Spring Framework

Writing AOP in Spring

Rate this Entry
by , 11-16-2011 at 05:35 PM (1016 Views)
In this last installment of Spring AOP, we will provide an example of how to use AOP in Spring.
In the listing below, we add a car to an order.

Java Code:
package com.acme.springexamples.main;
import com.acme.springexamples.service.CarService;
import com.acme.springexamples.service.OrderService;
...
public class Main { public static void main(String[] args) {
	ApplicationContext context = 
		new ClassPathXmlApplicationContext("META-INF/spring/*.xml");
	OrderService orderService = (OrderService) context.getBean("orderService");
	CarService carService = (CarService)context.getBean("carService");
	Order order = new Order(); 
	Car car = carService.findCar("FordFiestaStudio"); 
	orderService.addCar(order, car, 1);
}
In this example we will look at the point cut expression Let’s use AspectJ to introduce the tracing functionality into each method in all the classes in the example. The pointcut expression shown below can be used to apply advice to all domain, controller and repos method that are executed.

package com.acme.springexamples.tracing;

Java Code:
import ...

public aspect TraceAspect { 
	private Logger logger = Logger.getLogger(TraceAspect.class);
	pointcut domainOp() 
		: execution(* com.acme.springexamples.domain..*.*(..))
		|| execution(com.acme.springexamples..*.new(..));
	pointcut controllerOp() 
		: execution(* com.acme.springexamples.web..*.*(..))
		|| execution(com.acme.springexamples.web..*.new(..));
	pointcut repositoryOp() 
		: execution(* com.acme.springexamples.service..*.*(..))
		|| execution(com.acme.springexamples.service..*.new(..));
	pointcut traced() 
		: domainOp() || controllerOp() || serviceOp();
		... advice to traced() ...
}
Now you need to configure the aspect with for tracing using using dependency injection. For this, I use the standard Spring approach, but with one caveat. AspectJ aspects are created by the AspectJ runtime. I need Spring to configure the aspect that AspectJ has created. For a singleton aspects such as the TraceAspect aspect above, AspectJ defines an aspectOf() method that returns the aspect instance. I tell Spring to use the aspectOf() method as a factory method for obtaining the aspect instance.

Java Code:
 <beans>      
      <bean name="TraceAspect"
        class="com.acme.springexamples.tracing.TraceAspect"
        factory-method="aspectOf">
        <property name="logger">
          <ref bean="Logger"/>
        </property>
      </bean>
      <bean name="Logger" 
        class="org.apache.log4j.Logger">
      </bean>
  </beans>
In Summary, Spring doesn’t use the <bean> declaration to create an instance of the TraceAspect—it has already been created by the AspectJ runtime. Instead, Spring retrieves a reference to the aspect through the aspectOf() factory method and then performs dependency injection on it as prescribed by the <bean> element. There is much more you can do with Spring AOP. Check documentation for more information.

Submit "Writing AOP in Spring" to Facebook Submit "Writing AOP in Spring" to Digg Submit "Writing AOP in Spring" to del.icio.us Submit "Writing AOP in Spring" to StumbleUpon Submit "Writing AOP in Spring" to Google

Comments