Spring框架中采用AOP模式开发的探究
在软件开发过程中,可通过预编译方式和运行期间动态代理实现程序功能的统一维护,这种技术我们称之为AOP,全程为Aspect Oriented Programming,意为面向切面编程。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
如果我们在同一个方法上自定义多个切面、切点,他们默认的执行顺序是什么呢?
场景:单个切面
示例代码:
- @Component
- @Aspect
- public class TestAspect {
- @Pointcut("@within(org.springframework.stereotype.Service)")
- public void pointCut() {
- }
- @Around(value = "pointCut()")
- public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
- System.out.println("我是环绕通知.....");
- return joinPoint.proceed();
- }
- @Before(value = "pointCut()")
- public void beforeAdvice(JoinPoint joinPoint) {
- System.out.println("我是前置通知.....");
- }
- @After(value = "pointCut()")
- public void afterAdvice(JoinPoint joinPoint) {
- System.out.println("我是后置通知.....");
- }
- @AfterReturning(value = "pointCut()")
- public void afterReturning(JoinPoint joinPoint) {
- System.out.println("我是返回后通知.....");
- }
- @AfterThrowing(value = "pointCut()")
- public void after