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
- Spring AOP – Aspects Ordering
- [AOP] 3. Spring AOP中提供的种种Aspects
- [AOP] 4. Spring AOP中提供的种种Aspects
- [AOP] 5. Spring AOP中提供的种种Aspects
- Jboss AOP术语与Aspects 实现
- iOS的AOP(面向切面)编程--Aspects
- Aspects iOS的AOP面向切面编程的库
- Aspects iOS的AOP面向切面编程的库
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- 解决 Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects
- AOP、Spring的AOP
- AOP--Spring AOP
- Spring AOP 嵌套AOP
- spring AOP
- Spring AOP
- Spring AOP
- Android系统启动流程(nougat7.1.1_r6)
- HDU 1224 Free DIY Tour——DP
- JZOJ 5068.树
- Mongodb参数详解(参考:http://blog.csdn.net/freebird_lb/article/details/8229567)
- VScode‘s Extension for Evernote——EverMonkey
- Spring AOP – Aspects Ordering
- 数据分析职位爬虫与分析
- 【C语言】【unix c】程序中的错误处理
- 在Oracle中使用JSON: PL/JSON
- 帮助knn1找到路径
- GUI程序设计框架要点
- wmware虚拟机页面无法完整显示解决
- python生成器详解
- TCP程序流程及服务器客户端