6.aop AspectJ

Posted 一拳超人的逆袭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.aop AspectJ相关的知识,希望对你有一定的参考价值。

配置文件或者配置类

@Configuration
@ComponentScan(basePackages ={"day506AOP"})
@EnableAspectJAutoProxy(proxyTargetClass =true)
public class CofigAop {
}
<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="day506AOP"></context:component-scan>
    <!--开启aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

实体类

@Component
public class Stu {
    public void add(){
        System.out.println("add....");
    }
}

stuProxy

@Component
@Aspect
@Order(2)
public class StuProxy {
    //相同切入点抽取
    @Pointcut(value ="execution(* day506AOP.anno.Stu.add(..))")
    public void pointCut(){

    }
    //公共切入点使用
    @Before(value = "pointCut()")
    public void before(){
        System.out.println("before....");
    }
    //在方法执行之后执行 不管有没有异常都执行也可以叫做最终通知
    @After(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void after(){
        System.out.println("after....");
    }
    //在方法返回结果之后执行 晚于after 抛异常就不执行
    @AfterReturning(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning....");
    }
    //异常通知也是在after之后执行
    @AfterThrowing(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing....");
    }
    //环绕通知抛异常后不执行环绕之后但是执行环绕之前
    @Around(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前....");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后....");
    }
}

stuProxy2

@Component
@Aspect
@Order(1)
public class StuProxy2 {
    @Before(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void before(){
        System.out.println("before 先执行......");
    }
    @After(value = "execution(* day506AOP.anno.Stu.add(..))")
    public void after(){
        System.out.println("after 后执行......");
    }
}

测试类:

public class AOPIntro {
    @Test
    public void testAnno(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("506bean8.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.add();
    }
    //完全注解
    @Test
    public void testCompleteAnno(){
        ApplicationContext context=new AnnotationConfigApplicationContext(CofigAop.class);
        Stu stu = context.getBean("stu", Stu.class);
        stu.add();
    }

}

测试结果:

before 先执行......
环绕之前....
before....
add....
环绕之后....
after....
afterReturning....
after 后执行......

Process finished with exit code 0

 

以上是关于6.aop AspectJ的主要内容,如果未能解决你的问题,请参考以下文章

AOP之AspectJ - 代码注入

AOP之AspectJ - 代码注入

Guice vs AspectJ

Android AOP编程之AspectJ

Android AOP编程之AspectJ

Android AOP编程之AspectJ