Spring AOP – Aspects Ordering

来源:互联网 发布:c语言笔试题和答案 编辑:程序博客网 时间:2024/06/06 05:53

http://howtodoinjava.com/spring/spring-aop/spring-aop-specifying-aspects-ordering/


In our last tutorial, we learned about spring aop key terms and example. These we created a logging aspect and then applied onUserManager class. Suppose you have multiple aspects in your application and they are can be applied on a certain method. When there’s more than one aspect applied to the same join point, the precedence/order of the aspects will not be determined unless you have explicitly specified it using either @Order annotation ororg.springframework.core.Ordered interface. In this example, we will see an example of ordered aspects.

Specifying Aspects Ordering

As mentioned, to specify the order of aspects you have two ways:

1) Specifying aspects ordering using @Order annotation

This one is pretty simple. Use the annotation as below.

@Aspect@Order(0)public class EmployeeCRUDTransactionAspect{@Before("execution(* EmployeeManager.getEmployeeById(..))")public void getEmployeeById(JoinPoint joinPoint) {System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : " + joinPoint.getSignature().getName());}}@Aspect@Order(1)public class EmployeeCRUDLoggingAspect {@Before("execution(* EmployeeManager.getEmployeeById(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());}}

2) Specifying aspects ordering by implementing org.springframework.core.Ordered interface

This one is too equally easy.

@Aspectpublic class EmployeeCRUDLoggingAspect implements Ordered {//Override this methodpublic int getOrder() {return 0;}@Before("execution(* EmployeeManager.getEmployeeById(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());}}@Aspectpublic class EmployeeCRUDTransactionAspect implements Ordered {//Override this methodpublic int getOrder() {return 1;}@Before("execution(* EmployeeManager.getEmployeeById(..))")public void getEmployeeById(JoinPoint joinPoint) {System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : " + joinPoint.getSignature().getName());}}

Now it’s time to test if ordering works. Configure both aspects in applicationContext.xml file.

<aop:aspectj-autoproxy /><context:component-scan base-package="com.howtodoinjava.demo.aop" /><bean id="transactionAspect" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" /><bean id="loggingAspect" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

Let’s run the example as below:

public class TestAOP{@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml");EmployeeManager manager = context.getBean(EmployeeManager.class);manager.getEmployeeById(1);}}Output:EmployeeCRUDAspect.logBefore() : getEmployeeByIdEmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeByIdMethod getEmployeeById() called

原创粉丝点击