AspectJ注解
通过注解实现AOP操作
1.创建类,在类里面定义方法
2.创建增强类(编写增强逻辑)
(1)在增强类里面,创建方法,让不同方法代表不同通知
3.进行通知的配置
(1)在spring配置文件中,开启注解扫描
(2)使用注解创建User和UserProxy对象
(3)在增强类上面添加注解@Aspect
(4)在spring配置文件中开启生成代理对象
4.配置不同类型的通知
(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
5.相同的的切入点抽取
6.有多个增强类多同一个方法进行增强,设置增强类优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高
代码示例
代码示例:
增强类:
//增强的类 @Component @Aspect //生成代理对象 @Order(3) public class UserProxy { //相同切入点抽取 @Pointcut(value = "execution(* com.aop.spring5.aopanno.User.add(..))") public void pointdemo(){ } //前置通知 //@Before注解表示作为前置通知 @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("环绕之前......"); //被增强的方法执行 proceedingJoinPoint.proceed(); System.out.println("环绕之后......"); } }
被增强类:
//被增强的类 @Component public class User { public void add(){ System.out.println("add......"); } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.aop"></context:component-scan> <!--开启Aspect生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
通过xml方式实现AOP操作
AOP操作(AspectJ配置文件)
1.创建两个类,增强类和被增强类,创建方法
2.在spring配置文件中创建两个类对象
3.在spring配置文件中配置切入点
代码示例
示例代码:
增强类:
public class BookProxy { public void before(){ System.out.println("before........."); } }
被增强类:
public class Book { public void buy(){ System.out.println("buy............"); } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--创建对象--> <bean id="book" class="com.aop.spring5.aopxml.Book"></bean> <bean id="bookProxy" class="com.aop.spring5.aopxml.BookProxy"></bean> <!--配置aop增强--> <aop:config> <!--切入点--> <aop:pointcut id="p" expression="execution(* com.aop.spring5.aopxml.Book.buy(..))"/> <!--配置切面--> <aop:aspect ref="bookProxy"> <!--增强作用在具体的方法上--> <aop:before method="before" pointcut-ref="p"></aop:before> </aop:aspect> </aop:config> </beans>