spring aop 注解入门

面向方法的切面:

执行前:对入参D inData或者调用者进行处理

执行后:对返回值T 或者调用者进行处理

异常处理:对获取的异常或调用者进行处理

执行之中:对调用者,入参,返回值,以及异常进行处理--很强大,其实直接用这个就行。

 

小例子:

1.有一个业务,很简单,对传入的参数进行处理,然后返回

 

Java代码   收藏代码
  1. package com.aop.service;  
  2.   
  3.   
  4. import org.springframework.stereotype.Component;  
  5.   
  6. import com.aop.bo.InputVal;  
  7. import com.aop.bo.ReturnVal;  
  8.   
  9. @Component  
  10. public class ServiceImpl {  
  11.     public ReturnVal getData(InputVal val) throws Exception{  
  12.         ReturnVal rtVal = new ReturnVal();  
  13.         //模拟处理  
  14.         rtVal.setKey(val.getKey()+"处理后");  
  15.         rtVal.setValue(val.getValue()+"处理后");  
  16.         System.out.println("------------执行中-------------");  
  17.         //模拟异常  
  18. //      if(val.getKey().equals("ex")){  
  19. //          throw new Exception("异常!");  
  20. //      }  
  21.         return rtVal;  
  22.     }  
  23.       
  24.     public String toString() {  
  25.         return "ServiceImpl调用者";  
  26.     }  
  27.   
  28. }  
 

2.入参数据:

 

Java代码   收藏代码
  1. package com.aop.bo;  
  2.   
  3.   
  4. public class InputVal {  
  5.     private String key = null;  
  6.     private String value = null;  
  7.     public String getKey() {  
  8.         return key;  
  9.     }  
  10.     public void setKey(String key) {  
  11.         this.key = key;  
  12.     }  
  13.     public String getValue() {  
  14.         return value;  
  15.     }  
  16.     public void setValue(String value) {  
  17.         this.value = value;  
  18.     }  
  19.     @Override  
  20.     public String toString() {  
  21.         return "InputVal [key=" + key + ", value=" + value + "]";  
  22.     }  
  23.       
  24.       
  25. }  

 

 3.出参数据:

 

Java代码   收藏代码
  1. package com.aop.bo;  
  2.   
  3. public class ReturnVal {  
  4.     private String key = null;  
  5.     private String value = null;  
  6.       
  7.     public String getKey() {  
  8.         return key;  
  9.     }  
  10.     public void setKey(String key) {  
  11.         this.key = key;  
  12.     }  
  13.     public String getValue() {  
  14.         return value;  
  15.     }  
  16.     public void setValue(String value) {  
  17.         this.value = value;  
  18.     }  
  19.     @Override  
  20.     public String toString() {  
  21.         return "ReturnVal [key=" + key + ", value=" + value + "]";  
  22.     }  
  23.       
  24. }  
 

 

4.对业务进行切面:

 

Java代码   收藏代码
  1. package com.aop.aop;  
  2.   
  3.   
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5. import org.aspectj.lang.annotation.Around;  
  6. import org.aspectj.lang.annotation.Aspect;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import com.aop.bo.InputVal;  
  10. import com.aop.bo.ReturnVal;  
  11.   
  12. @Component  
  13. @Aspect  
  14. public class AopService {  
  15.       
  16. //  @Pointcut("execution(* com.aop.service.ServiceImpl.getData(..))")  
  17. //  private void cut(){}  
  18.     private final String CUT = "execution(* com.aop.service.ServiceImpl.getData(..))";  
  19.       
  20.     /* 
  21.     //方法执行前调用 
  22.     @Before(CUT) 
  23.     public void before(){ 
  24.         System.out.println("-----------执行前--------------------"); 
  25.     } 
  26.      
  27.     //方法执行后调用   
  28.     @After(CUT)   
  29.     public void after() {   
  30.         System.out.println("------------执行后------");   
  31.     } 
  32.     */  
  33.     @Around(CUT)   //spring中Around通知  环绕通知  
  34.     public Object logAround(ProceedingJoinPoint joinPoint) {  
  35.         Object tarjet = joinPoint.getTarget();//1.调用者  
  36.         //-----日志,调用者  
  37.         System.out.println("1.调用者:------"+tarjet);  
  38.           
  39.         Object[] args = joinPoint.getArgs();//2.传参  
  40.         System.out.println("2.传参:----"+args[0]);  
  41.           
  42.         if(args[0instanceof InputVal){  
  43.             InputVal val = (InputVal) args[0];  
  44.             val.setKey(val.getKey()+"改了么?");  
  45.         }  
  46.           
  47.         Object obj = null;  
  48.         try {  
  49.             obj = joinPoint.proceed(args);  
  50.             if(obj.getClass().equals(ReturnVal.class)){  
  51.                 ReturnVal rval = (ReturnVal) obj;  
  52.                 rval.setValue(rval.getValue()+"改了?");  
  53.             }  
  54.         } catch (Throwable e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.           
  58.         System.out.println("3.返回参数:---"+obj);//3.返回参数  
  59.           
  60.         return obj;  
  61.     }   
  62. }  

 

 5.调用测验:

 

Java代码   收藏代码
  1. package com.aop.test;  
  2.   
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.aop.bo.InputVal;  
  8. import com.aop.service.ServiceImpl;  
  9.   
  10. public class Main {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      * @throws Exception  
  15.      */  
  16.     public static void main(String[] args) throws Exception {  
  17.         ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");  
  18.           
  19.         ServiceImpl si = (ServiceImpl) ctx.getBean("serviceImpl");  
  20.           
  21.         InputVal val = new InputVal();  
  22.         val.setKey("inKey");  
  23.         val.setValue("inVal");  
  24.         System.out.println(si.getData(val));  
  25.     }  
  26.   
  27. }  

 

 6.配置文件,要打开aop注解扫描:

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.  http://www.springframework.org/schema/context  
  9.  http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.  http://www.springframework.org/schema/aop  
  11.  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  12.       
  13.    
  14.     <!-- 扫描com.ioc下的所有类,自动为spring容器管理 -->  
  15.     <context:component-scan base-package="com"/>  
  16.      <!-- 打开面向切面工具 -->  
  17.      <aop:aspectj-autoproxy/>  
  18. </beans>  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值