Spring的AOP开发-AOP简介
AOP的概念
AOP,面向切面编程,是对面向对象编程OOP的升级。OOP的纵向对一个事物的抽象,一个对象包括静态的属性信息,包括动态的方法信息等。而AOP是横向的对不同事物的抽象,属性与属性、方法与方法、对象与对象都可以组成一个切面,用这种思维设计编程的方式叫面向切面编程
AOP思想的实现方案-代理技术
动态代理技术,在运行期间,对目标对象的犯法进行增强,代理对象同名方法内可以执行原有逻辑的同时嵌入执行其他增强逻辑或其他对象的方法
A对象:
methodA1()
methodA2()
methodA3()
B对象:
methodB1()
methodB2()
A对象Proxy对象
methodA1() {
B对象的methodB1();
目标对象A的methodA1();
B对象的methodB2();
}
methodA2() {
B对象的methodB1();
目标对象A的methodA2();
B对象的methodB2();
}
模拟AOP思想实现方案
在Spring的后处理器之BeanPostProcessor博客(https://blog.csdn.net/weixin_42594143/article/details/128872629),案例:对Bean方法进行执行时间日志增强中我们使用过动态代理对Bean进行增强,实际存储到单例池singleObjects中的不是当前目标对象本身,而是当前目标对象的代理对象Proxy,这样在调用目标对象方法时,实际调用的是代理对象Proxy的同名方法,起到了对目标方法前后都进行增强的功能,对该方法进行优化,将增强的方法提取出去到一个增强类中,且只对com.luxifa.service.impl包下的任何类的任何方法进行增强
package com.luxifa.service;
public interface UserService {
void show1();
void show2();
}
package com.luxifa.service.impl;
public class UserServiceImpl implements UserService {
@Override
public void show1() {
System.out.println("show1...")
}
@Override
void show2() {
System.out.println("show2...")
}
}
package com.luxifa.advice;
//增强类.内部提供增强方法
public class MyAdvice {
public void beforeAdvice() {
System.out.println("前置的增强...");
}
public void afterAdvice() {
System.out.println("后置的增强...");
}
}
public class MockAopBeanPostProcessor implements BeanPostProcessor,ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public Object postProcessAfterInitialization (Object bean,String beanName) throws BeanException {
//目的:对UserServiceImpl中的show我和show2方法进行增强,增强方法存在MyAdvice中
//问题1:筛选service.impl包下所有的类的所有方法都可以进行筛选,解决方案if-else
//问题2:MyAdivece怎么获取到?解决方案:从Spring容器中获得MyAdvice
if(bean.getClass().getPackage().getName().equals("com.luxifa.service.impl")) {
//生成当前Bean的Proxy对象
Object beanProxy = Proxy.newProxyInstance(
bean.getClass().getClassLoader(),
bean.getClass().getInterfaces(),
(Object proxy,Method method,Object[] args)->{
MyAdvice myAdvice = applicationContext.getBean(MyAdvice.class);
//执行增强对象的before方法
myAdvice.beforeAdvice();
//执行目标对象的目标方法
Object result = method.invoke(bean,args);
//执行增强对象的after方法
myAdivce.afterAdvice();
return result;
}
);
return beanProxy ;
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeanException {
this.applicationContext = applicationContext;
}
}
xm中
<bean id="userService" class="com.luxifa.service.impl.UserServiceImpl"></bean>
<bean id="myAdvice" class="com.luxifa.advice.MyAdive"></bean>
<bean class="com.luxifa.MockAopBeanPostProcessor"></bean>
测试类:
public class ApplicationContextTest {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.show1();
}
}
控制台打印:
前置的增强....
show1...
后置的增强....
AOP相关概念
概念 | 单词 | 解释 |
---|---|---|
目标对象 | Target | 被增强的方法所在的对象 |
代理对象 | Proxy | 对目标对象进行增强后的对象,客户端实际调用的对象 |
连接点 | JoinPoint | 目标对象中可以被增强的方法 |
切入点 | Pointcut | 目标对象中实际被增强的方法 |
通知\增强 | Advice | 增强部分的代码逻辑 |
切面 | Aspect | 增强和切入点的组合 |
织入 | Weaving | 将通知和切入点组件动态组合的过程 |