zoukankan      html  css  js  c++  java
  • AspectJ注解和AspectXML

    1.AspectJ是什么?

    AspectJ是一个基于Java语言的AOP框架,Spring2.0开始,Spring AOP引入对AspectJ的支持,AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入。

        AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供代码的织入

        @AspectJAspectJ 5新增的功能,使用JDK 5.0 注解技术和正规的AspectJ切点表达式语言描述切面

         Spring通过集成AspectJ实现了以注解的方式定义增强类,大大减少了配置文件中的工作量

         使用@AspectJ,首先要保证所用的JDK 5.0或以上版本

    Springasm 模块添加到类路径中,以处理@AspectJ中所描述的方法参数名

    2.使用AspectJ注解增强

    定义增强类  实体类加@Aspect就变为增强类

    注解一共有五种:

    1)@Before  前置增强

    2)@AfterReturning  后置增强

    3)@Around 环绕增强

    4)@AfterThrowing  异常增强

    5)@After  最终增强  不管怎么样 都会执行一次

    @Aspect
    public class aspectAdvice {
    /*    //前置增强
        @Before("execution(* DemoAspect.*.doSome(..))")
        public void myBefore(){
            System.out.println("===before===");
        }
        //后置增强
        @AfterReturning("execution(* DemoAspect.*.doGet(..))")
        public void myAfter(){
            System.out.println("===end===");
        }*/
        //环绕增强
        @Around("execution(* DemoAspect.*.sayhi(..))")
        public void myArpund(ProceedingJoinPoint point) throws Throwable {
            System.out.println("我在上边");
            point.proceed();
            System.out.println("我在下边");
        }
        //异常增强
        @AfterThrowing("execution(* DemoAspect.*.doSome(..))")
        public void MyThrowing(){
            System.out.println("异常出现了!");
        }
        //最终增强
        @After("execution(* DemoAspect.*.Ends(..))")
        public void MyEnd(){
            System.out.println("我是最终增强");
        }
    }

    配置aspet自动代理:    xml文件中加入aop节点

    <!--目标类型-->
        <bean id="service" class="DemoAspect.SomeBeforeImpl"></bean>
    
    <!--增强通知-->
        <bean id="beforeadvice" class="DemoAspect.aspectAdvice"></bean>
    
        <!--aspectJ自动代理-->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    书写测试类:

        //AspectJ注解  前置  后置  环绕  异常 最终  增强
        @Test
        public void Test03(){
           ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextdayaspect.xml") ;
            DemoAspect.SomeBefore service = (DemoAspect.SomeBefore)context.getBean("service");
          service.Ends();
        }

    3.AspectJ使用XML

    步骤:

    1)定义一个普通类,类可以不用实现任何接口

    2)书写增强的方法 

        //AspectJ注解  前置  后置  环绕  异常 最终  增强
        @Test
        public void Test03(){
           ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextdayaspect.xml") ;
            DemoAspect.SomeBefore service = (DemoAspect.SomeBefore)context.getBean("service");
          service.Ends();
        }

    3)配置XML文件

    <!--目标类型-->
        <bean id="service" class="DamoAspectXml.SomeBeforeImpl"></bean>
    
    <!--增强通知-->
        <bean id="beforeadvice" class="DamoAspectXml.AspectAdvice"></bean>
    
    
        <!--aspect xml 版本-->
        <aop:config>
            <aop:pointcut id="mypoint" expression="execution(* DamoAspectXml.*.*(..))"></aop:pointcut>
            <aop:aspect ref="beforeadvice">
                <aop:before method="myBefore" pointcut-ref="mypoint"></aop:before>
                <!--<aop:after-returning method="afterReturing" pointcut-ref="mypoint"></aop:after-returning>-->
            </aop:aspect>
        </aop:config>

    4)测试类代码:

      //aspectJ xml版本
        @Test
        public void Test04(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextdayaspectxml.xml") ;
            DamoAspectXml.SomeBefore service = (DamoAspectXml.SomeBefore)context.getBean("service");
            service.doGet();
            service.doSome();
        }
  • 相关阅读:
    ASP.NET Core 搭配 Nginx 的真实IP问题
    【Spark】Sparkstreaming-性能调优
    【软件】软件设计-非功能性需求-性能-可靠性-安全性等
    【Spark】Spark-架构
    【Kafka】Kafka-副本-分区设置-性能调优
    【Spark】SparkStreaming-Tasks-数量如何设置?
    【Spark】Spark-性能调优-系列文章
    【Spark】Spark-空RDD判断与处理
    【Git】Git-add之后-忽略部分文件的方法
    【大数据】Spark-Hadoop-架构对比
  • 原文地址:https://www.cnblogs.com/1234AAA/p/8550767.html
Copyright © 2011-2022 走看看