1:什么是 AOP
AOP就是面向切面编程。首先它是一种编程思想,是OOP的一种补充。OOP代码逻辑一般都是自上而下的,在自上而下的过程会产生一些横切性问题,AOP就是处理一些横切性问题,AOP就是把横切性问题和主业务逻辑分开,达到解耦的目的。使代码的重用性和开发效率更高。
2:aop中的一些概念
aspect:交给spring去管理
pointcut:切入点
Joinpoint:连接点 JoinPoint是要关注和增强的方法,也就是我们要作用的点
Weaving :把代理逻辑加入到目标对象上的过程叫做织入
target 目标对象 原始对象aop
Proxy 代理对象 包含了原始对象的代码和增加后的代码的那个对象
advice:通知
3:小例子
@Pointcut 申明切入点,匹配TestAspect所有方法调用 ,该方法 pintCut的内容并不重要,方法名也不重要,实际上它只是作为一个标识,供通知使用 。
execution 匹配方法执行连接点
@Before 申明before通知,在pintCut切入点前执行
@Component
@Aspect
public class LiuJunShuAspect {
@Pointcut("execution(* cn.dotfashion.soa.voyager.service.TestAspect.*(..))")
public void pintCut() {
}
@Before("cn.dotfashion.soa.voyager.aspect.LiuJunShuAspect.pintCut()")
public void beforeAdvice(){
System.out.println("before");
}
}
@Repository
public class TestAspect {
public void test(){
System.out.println(“少女的梦”);
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(LiuJunShuAspect.class);
TestAspect testAspect=annotationConfigApplicationContext.getBean(TestAspect.class);
testAspect.test();
}
结果输出:before
少女的梦