AOP核心概念梳理:
- 代理模式
- 切面三种实现方式
- 通过切面实现事务增强
静态代理案例:
/*
* 特点:代理类和被代理类在编译期间,就已经确定
*/
interface ClothFactory{
void produceCloth();
}
//被代理类
class LiningClothFactory implements ClothFactory{
@Override
public void produceCloth() {
System.out.println("Lining工厂生产一批运动服");
}
}
//代理类
class ProxyClothFactory implements ClothFactory{
private ClothFactory factory;//用被代理类对象进行实例化
public ProxyClothFactory(ClothFactory factory){
this.factory = factory;
}
@Override
public void produceCloth() {
System.out.println("代理工厂的准备工作");
factory.produceCloth();
System.out.println("代理工厂后续的收尾工作");
}
}
public class StaticProxyTest {
public static void main(String[] args) {
//创建被代理类的对象
LiningClothFactory Lining = new LiningClothFactory();
//创建代理类的对象
ProxyClothFactory proxyClothFactory = new ProxyClothFactory(Lining);
proxyClothFactory.produceCloth();
}
}
Spring AOP 切面案例:
需知:Spring AOP 关于切点表达式:
权限修饰符 权限定路径 方法名 (参数类型)
execution(public * *