什么是AOP
百度百科:
回想上一篇写的静态代理和动态代理,有共同的抽象类(租房的抽象方法),真实对象(Host房东),代理(Proxy中介),还有我们编写的继承了InvocationHandler的类(增强类)
aop的图
按照我们的(租房)案例,解释
- 目标对象,就是真实对象(房东)
- 切面,就是我们的InvocationHandlerProxy
AOP在Spring中的作用,
较官方的解释
结合租房案例进行白话解释
- 允许用户自定义切面:用户向增加什么功能,就把它提取成一个类,用AOP实现
- 切面:就是InvocationHandlerProxy类
- 通知:就是我们在InvocationHandlerProxy类里写的lookhouse()或fare()方法
- 目标:target,就是我们的真实对象
- 代理:就是我们在测试类里写的 Rent proxy=(Rent)ihp.getProxy();
- 切入点:考虑是在【代理】之前执行还是之后执行
- 连接点:把真实对象和切面关联的点,就是我们在测试类里写的 ihp.setRent(host)
**SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice
代码实现
三种方式
- 使用SpringAPI实现AOP
编写接口
public interface UserService {
void add();
void delete();
void update();
void query();
}
编写实现类
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void query() {
System.out.println("查询用户");
}
}
定义日志增强类实现
public class log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//objects:要被调用的方法的参数
//o:目标对象 (就是target)
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
//method:被调用的方法
//args:被调用的方法对象的参数
//target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+
"返回值"+returnValue);
}
}
编写Spring核心配置文件【导入约束】
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<!--注册日志类的bean-->
<bean id="log" class="com.kuang.log.log"/>
<bean id="afgerLog" class="com.kuang.log.AfterLog"/>
<!--使用Spring的aop切入
1、导入约束
2、aop:config
-->
<aop:config>
<!--切入点
expression 表达式,表示要切入的位置
语法:execution([类的修饰符][类的全路径][方法][参数])
-->
<aop:pointcut id="point" expression
="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--执行通知,增强-->
<aop:advisor advice-ref="log" pointcut-ref="point"/>
<aop:advisor advice-ref="afgerLog" pointcut-ref="point"/>
</aop:config>
</beans>
测试类【需要导入AOP的织入包】(aspectjweaver)
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.3</version>
</dependency>
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
/*
问题:报错,没有aspectjweaver包
使用aop需要导入一个包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
* 问题:Spring调用的是真实对象userService
* 暗箱中:动态的修改userService,在方法的前后或者其他通知的地方增加了我们的切入代码
* 我们就可以实现依旧调用原来的对象,产生增加的新的业务的功能;
* */
userService.add();
}
* 理解其横向编程的思想 *
* Spring的AOP即使将公共业务代码(日志,事务,安全。。),和业务类(真实对象)结合起来实现公共业务的重复利用,(解耦)
- 自定义类实现AOP
较上面的方法简单- 真实对象和之前一样
- 自定义一个aop增强类(diy):切面
public class Diy {
public void before(){
System.out.println("===========方法执行前===============");
}
public void after(){
System.out.println("===========方法执行后===============");
}
}
注入bean
(使用aop进行增强(织入))
<!--注册bean-->
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<!--自定义aop增强类-->
<bean id="diy" class="com.kuang.diy.Diy"/>
<!--编写aop配置文件-->
<aop:config>
<!--切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="diyPointCut" expression=
"execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="diyPointCut"/>
<aop:after method="after" pointcut-ref="diyPointCut"/>
</aop:aspect>
</aop:config>
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beas.xml");
UserService diy = (UserService) context.getBean("userService");
diy.add();
}
结果
===========方法执行前===============
增加用户
===========方法执行后===============
Process finished with exit code 0
- 使用注解实现AOP
@before @after- 目标对象不变
- 编写增强的类,写注解
- 注意点:需要将类注解为切面
- 方法上就是切入点,增强
//必须要写切面注解,否则就无法切入
@Aspect
public class Anno {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前====");
}
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后====");
}
//环绕增强
@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp){
System.out.println("环绕增强前");
// System.out.println("签名:"+jp.getSignature());
//执行目标方法
}
}
配置文件
<!--注册bean-->
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<!--注解实现AOP的类-->
<bean id="anno" class="com.kuang.anno.Anno"/>
<!--识别注解,自动代理-->
<aop:aspectj-autoproxy/>
测试
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("anno.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
~~============================================================~~
AOP小结
- 本质就是动态代理
- 需要导入一个包进行aop织入的包:aspectjweaver
- 注意别遗漏了切面
- 三种实现aop的方法
- 使用SpringAPI来实现AOP
- 使用自定i类来实现AOP
- 使用注解来实现AOP