zoukankan      html  css  js  c++  java
  • 注解定义增强的两个方法

      

          1    @AspectJ  

        

     1 public class Lpg  {
     2 
     3     @Pointcut("execution(* *..autowired.dao.impl.Seppp.insert(..))")
     4     public  void inse(){
     5 
     6     }
     7 
     8      @Before("execution(* *..autowired.dao.impl.Seppp.select(..))")
     9     public void befo(){
    10          System.out.println("aaaaas");
    11      }
    12 
    13      @AfterReturning("inse()")
    14     public void after(){
    15          System.out.println("bbbbbbbb");
    16      }
    17 
    18      @AfterThrowing("execution(* *..autowired.dao.impl.Seppp.select(..))")
    19     public void ing(){
    20          System.out.println("Exception");
    21      }
    22 
    23      @Around("execution(* *..autowired.dao.impl.Seppp.select(..))")
    24      public  void around(ProceedingJoinPoint jion) throws Throwable {
    25          System.out.println("前");
    26          jion.proceed();
    27          System.out.println("后");
    28      }
    29 
    30      @After("execution(* *..autowired.dao.impl.Seppp.select(..))"  )
    31      public  void ss(){
    32          System.out.println("end");
    33      }
    34 }

     这是使用@Aspect    使用进行增强注解    

       前      后     环绕    最终      异常    这五种增强

    @Befo     @AfterThrowing  

    @Around
    @After
    @AfterThrowing   在注解内写入切面表达式    匹配    方法        使用环绕分割前后   参数 ProceedingJoinPoint  在之中调用 .proceed(); 的方法就行。

    现在开始写 配置文件

    还写一个类来进行测试

    public interface Sel {
    
        public void select();
    
        public void insert();
    
    }
    public class Seppp implements Sel {
    
        public void select() {
          ///  int i=1/0;
            System.out.println("select");
        }
    
        public void insert() {
            System.out.println("insert");
        }
    }

    以上为测试的类 

    小配置 

    第一步,把增强注解和测试的类    写出bean节点    

    第二部 ,写入一个aop的配置节点  

        <bean id="service" class="cn.autowired.dao.impl.Seppp"></bean>
     <bean id="pp" class="cn.autowired.dao.Lpg"></bean>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

        现在开始写测试类 

     1  @Test
     2     public void dgh() {
     3         ApplicationContext context = new ClassPathXmlApplicationContext("application-05.xml");
     4         Sel ss = (Sel) context.getBean("service");
     5         try {
     6             ss.select();
     7         }
     8         catch(Exception e){
     9             e.printStackTrace();
    10         }
    11 
    12     }

     测试成功之后就会  ↓ 

    三月 13, 2018 8:43:27 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Tue Mar 13 08:43:27 CST 2018]; root of context hierarchy
    三月 13, 2018 8:43:27 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [application-05.xml]
    前
    aaaaas
    select
    后
    end

      前面还看见了一个

     @Pointcut的注解  , 这个是 是简写下面的    后面依旧是匹配切面的表达式  下面随意定一个方法  之后  在别的注解标签里写这个方法名就行  ,详细在第一张图   ↑

          现在再来讲一个  xml版本的   就是通过两个普通类     来进行增强   

       

    public interface textc {
    public void look();
    
    }
    public class text implements textc {
    
        public void look() {
    
            System.out.println("look");
            int i=1/0;
        }
    }
    public class Zq {
    
        public void befor(){
            System.out.println("===========befo");
        }
    
        public void afterreturning(){
            System.out.println("===========afterreturning");
        }
    
        public void around(ProceedingJoinPoint point) throws Throwable {
            System.out.println("=========around");
            point.proceed();
            System.out.println("=======around");
        }
    
        public void after(){
            System.out.println("==========after");
        }
    
        public void throwing(){
            System.out.println("===========throwing");
        }
    
    }

    现在开始小配置 ,

    因为这种实现方法,

    注重配置文件的书写

      <bean id="befor" class="cn.autowired.entity.Zq"></bean>
    
    
        <bean id="text" class="cn.autowired.entity.text"></bean>

    这两个类的bean节点肯定是有的  

       <aop:config>
            <aop:pointcut id="mypoint" expression="execution(* *..autowired.entity.text.look(..))"></aop:pointcut>
    
            <aop:aspect ref="befor">
    
                <aop:before method="befor" pointcut-ref="mypoint"></aop:before>
                <aop:after-returning method="afterreturning" pointcut-ref="mypoint"></aop:after-returning>
                <aop:after method="after" pointcut-ref="mypoint"></aop:after>
                <aop:around method="around" pointcut-ref="mypoint"></aop:around>
                <aop:after-throwing method="throwing" pointcut-ref="mypoint"></aop:after-throwing>
            </aop:aspect>
    
        </aop:config>

    这个就是这种写法的关键了,

    aop:pointcut  是匹配的   
    aop:aspect    ref为增强方法bean节点id     

    aop before after around after-throwinng after-returning 为 前后环绕最终异常 增强

    method 的属性值为 Zq方法的方法名 pointcut 的值为 aop:pointcut 的属性值

    使用 ↓
    @Test
        public  void tt(){
            ApplicationContext context = new ClassPathXmlApplicationContext("application-05.xml");
            textc ss = (textc) context.getBean("text");
            try {
    
    
           ss.look();
        }catch (Exception e){
                e.printStackTrace();;
    
            }
        }
    这就实现了增强  
  • 相关阅读:
    实用机器学习 跟李沐学AI
    Explicitly drop temp table or let SQL Server handle it
    dotnettransformxdt and FatAntelope
    QQ拼音输入法 禁用模糊音
    (技术八卦)Java VS RoR
    Ruby on rails开发从头来(windows)(七)创建在线购物页面
    Ruby on rails开发从头来(windows)(十三)订单(Order)
    Ruby on rails开发从头来(windows)(十一)订单(Order)
    新员工自缢身亡,华为又站到了风口浪尖
    死亡汽油弹(Napalm Death)乐队的视频和来中国演出的消息
  • 原文地址:https://www.cnblogs.com/LWLDD/p/8553502.html
Copyright © 2011-2022 走看看