概念
重点就是在不修改源代码的情况下实行代码增强或者时添加新的功能。
AOP举例
AOP术语
准备工作
1、Spring 框架一般都是基于 AspectJ 实现 AOP 操作
(1)AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作
2、基于 AspectJ 实现 AOP 操作
(1)基于 xml 配置文件实现
(2)基于注解方式实现(使用)
3、在项目工程里面引入 AOP 相关依赖
4、切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构:execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
举例1:对com.atguigu.dao.BookDao类里面的add进行增强execution(*com.atguigu.dao.BookDao.add(…))
举例2:对com.atguigu.dao.BookDao类里面的所有的方法进行增强execution(com.atguigu.dao.BookDao.(…))
基于注解的AOP开发
示例:
待增强的类:
package com.aop;
import org.springframework.stereotype.Component;
//待增强的类
@Component(value = "userName") //表示交给spring去创建和管理对象
public class User {
//待增强的方法
public void add() {
System.out.println("add");
throw new RuntimeException("错误");
}
}
代理类:
package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect //生成代理对象
public class Proxy {
/*前置通知,在被增强方法前执行
@Before(value = "execution(* com.aop.User.add(..))")
public void before() {
System.out.println("befor");
}
*/
/*最终通知,在被增强的方法后执行
@After(value = "execution(* com.aop.User.add(..))")
public void after() {
System.out.println("after");
}
*/
/*后置通知,在被增强的方法后执行,与after不同的时,如果被增强的方法发生了异常,
这个后置通知就不会再执行,但是after修饰的后置通知仍可以执行
@AfterReturning(value = "execution(* com.aop.User.add(..))")
public void afterReturn() {
System.out.println("afterReturn");
}
*/
/*
@Around(value = "execution(* com.aop.User.add(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("执行前");
//执行被增强的方法
point.proceed();
System.out.println("执行后");
}
*/
/*异常通知,在被增强的方法发生异常时执行
@AfterThrowing(value = "execution(* com.aop.User.add(..))")
public void afterThrow() {
System.out.println("执行");
}
*/
}
配置文件:
<?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.aop"></context:component-scan>
<!--开启AspectJ生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试类:
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void Test() {
ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");
User user = context.getBean("userName",User.class);
user.add();
}
}
利用配置类代替xml配置文件
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(Configure.class);
有多个增强类多同一个方法进行增强,设置增强类优先级
(1)在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高
@Component
@Aspect
@Order(1)
public class PersonProxy
相同的切入点抽取
//相同切入点抽取
@Pointcut(value = “execution(* com.atguigu.spring5.aopanno.User.add(…))”)
public void pointdemo() {
}
@Before(value = “pointdemo()”)
public void before() {
System.out.println(“before…”);
}
基于xml的AOP开发
1、创建两个类,增强类和被增强类
2、在 spring 配置文件中创建两个类对象
<!--创建对象-->
<bean> id="book" class="com.atguigu.spring5.aopxml.Book"></bean>
<bean>id="bookProxy"class="com.atguigu.spring5.aopxml.BookProxy"></bean>
3、在 spring 配置文件中配置切入点
<!--配置 aop 增强--> <aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(*
com.atguigu.spring5.aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"/>
</aop:aspect>
</aop:config>