spring-210728-02—AOP-AspectJ注解-抽取相同切入点&设置优先级
相同切入点抽取
UserProxy02.java
package com.bgy.spring.aoptest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class UserProxy02 {
@Pointcut(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
public void pointdemo(){
}
@Before(value = "pointdemo())")
public void before(){
System.out.println("before..........");
}
@After(value = "pointdemo())")
public void after(){
System.out.println("after.............");
}
@AfterReturning(value = "pointdemo())")
public void afterReturning(){
System.out.println("afterReturning.............");
}
@AfterThrowing(value = "pointdemo())")
public void afterThrowing(){
System.out.println("afterThrowing.............");
}
@Around(value = "pointdemo())")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("around 环绕之前.............");
proceedingJoinPoint.proceed();
System.out.println("around 环绕之后.............");
}
}
多个增强类,同一个方法进行增强,设置增强类优先级
在增强 类上面 添加 注解@Order(数字类型值) ,数字越小优先级越高
演示
Person.java
package com.bgy.spring.aoptest;
import org.springframework.stereotype.Component;
@Component
public class Person {
public void add(){
System.out.println("person add.......");
}
}
PersonProxy01.java
package com.bgy.spring.aoptest;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(2)
public class PersonProxy01 {
@Before(value = "execution(* com.bgy.spring.aoptest.Person.add(..))")
public void before(){
System.out.println("personProxy01 before........");
}
}
PersonProxy02.java
package com.bgy.spring.aoptest;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(1)
public class PersonProxy02 {
@Before(value = "execution(* com.bgy.spring.aoptest.Person.add(..))")
public void before(){
System.out.println("personProxy02 before........");
}
}
bean01.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.bgy.spring.aoptest"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
TestPerson.java
import com.bgy.spring.aoptest.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPerson {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
Person person = context.getBean("person", Person.class);
person.add();
}
}