博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring学习四
阅读量:4598 次
发布时间:2019-06-09

本文共 8913 字,大约阅读时间需要 29 分钟。

1先导入 Asject的jar包

2配置文件加入标签 ,并加入

<aop:aspectj-autoproxy proxy-target-class="true">(如果是实现接口模式的代理就不用后面的true)

</aop:aspectj-autoproxy>

3代理class加@Componet 和@Aspect,方法前面加@Before 当然不止Before 后面介绍

代码如下

package AOP;//接口类public interface CalculatorMethod {   int add(int x,int y);   int sub(int x,int y);   int mul(int x,int y);   int div(int x,int y);}package AOP;import org.springframework.stereotype.Component;@Componentpublic class Calculator implements CalculatorMethod {        public int add(int x,int y) {                return (x+y);    }    public int sub(int x,int y) {                return (x-y);    }    public int mul(int x,int y) {                return (x*y);    }    public int div(int x,int y) {                return (x/y);    }    }package AOP;//关键代理文件import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class Logging {            @Before("execution(int AOP.Calculator.add(int, int))")    /*     *   可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。     *      * */    public void log(){        System.out.println("I am out");    }}package AOP;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//在工程目录下读取bean的配置。        // CalculatorMethod cal = (CalculatorMethod) context.getBean("calculator");//这是基于接口形式 如果要基于cglib这种,则要修改xml配置文件加入proxy-target-class="true"         Calculator cal = (Calculator) context.getBean("calculator");         System.out.println(cal.add(8, 8));         System.out.println(cal.sub(8, 8));    }}/*输出I am out160*/

Bean文件添加

 

 

五种注解,@Before 前置,@After后置, @AfterReturning 返回,@AfterThrowing 异常, @Around环绕,具体关系看代码。

package AOP;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class Logging {            @Before("execution(int AOP.Calculator.add(int, int))")    /*     *   可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。     *      * */    public void log(){        System.out.println("I am out");    }        @After("execution(int AOP.Calculator.*(int, int))")//这里不能用返回值,因为返回值得再返回函数。    public void log2(JoinPoint jp){        String methodName = jp.getSignature().getName();        List l = Arrays.asList(jp.getArgs());        System.out.println("Method name: "+methodName+"  with "+l);            }        //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result")     @AfterReturning(returning="result"     , pointcut="execution(int AOP.Calculator.*(int, int))")    public void log3(JoinPoint jp,Object result){        String methodName = jp.getSignature().getName();        List l = Arrays.asList(jp.getArgs());        System.out.println("Method name: "+methodName+" end with "+result);    }          @AfterThrowing(throwing="e", pointcut="execution(int AOP.Calculator.*(int, int))")     public void log4(JoinPoint jp,Exception e){
//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。 String methodName = jp.getSignature().getName(); List l = Arrays.asList(jp.getArgs()); System.out.println("Method name: "+methodName+" with "+l+" error:"+e); } @Around("execution(int AOP.Calculator.*(int, int))")//相当于一次代理,用这个可以理解上面的过程 public Object log5(ProceedingJoinPoint pjp){ Object result =null; try { System.out.println("Around 前置"); //前置 result = pjp.proceed(); System.out.println("Around 后置");//后置 } catch (Throwable e) {
//异常 // TODO Auto-generated catch block System.out.println("Around 异常"); e.printStackTrace(); } System.out.println("Around 返回");//返回 return result; }}

 

 

切面的优先级 @Order 数值越小优先级越高,切入表达式 @Pointcut  声明表达式,不用加入其它方法,只是用于声明切入点,用的在其他方法里面加入该方法名字即可。其他class加class名字

代码简单如下

@Order(1)@Aspect@Componentpublic class Logging2 {        @Pointcut("execution(int AOP.Calculator.add(int, int))")    public void logPoint(){}        @After("logPoint()")    public void log1(){        System.out.println("afetr log 2");    }}
package AOP;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(2)@Aspect@Componentpublic class Logging {            @Before("Logging2.logPoint()")    /*     *   可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。     *      * */    public void log(){        System.out.println("I am out");    }        @After("Logging2.logPoint()")//这里不能用返回值,因为返回值得再返回函数。    public void log2(JoinPoint jp){        String methodName = jp.getSignature().getName();        List l = Arrays.asList(jp.getArgs());        System.out.println("Method name: "+methodName+"  with "+l);            }        //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result")     @AfterReturning(returning="result"     , pointcut="Logging2.logPoint()")    public void log3(JoinPoint jp,Object result){        String methodName = jp.getSignature().getName();        List l = Arrays.asList(jp.getArgs());        System.out.println("Method name: "+methodName+" end with "+result);    }          @AfterThrowing(throwing="e", pointcut="Logging2.logPoint()")     public void log4(JoinPoint jp,Exception e){
//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。 String methodName = jp.getSignature().getName(); List l = Arrays.asList(jp.getArgs()); System.out.println("Method name: "+methodName+" with "+l+" error:"+e); } @Around("Logging2.logPoint()")//相当于一次代理,用这个可以理解上面的过程 public Object log5(ProceedingJoinPoint pjp){ Object result =null; try { System.out.println("Around 前置"); //前置 result = pjp.proceed(); System.out.println("Around 后置");//后置 } catch (Throwable e) {
//异常 // TODO Auto-generated catch block System.out.println("Around 异常"); e.printStackTrace(); } System.out.println("Around 返回");//返回 return result; }}

 

 

基于xml的配置aspect切面

先声明切面的bean,之后配置AOP 用 aop:config

//删除所有注解注入方式public class Logging {                /*     *   可以缩略成execution(int AOP.Calculator.*(int, int)),代表这个包下所有方法都行。     *      * */    public void log(){        System.out.println("I am out");    }            public void log2(JoinPoint jp){        String methodName = jp.getSignature().getName();        List l = Arrays.asList(jp.getArgs());        System.out.println("Method name: "+methodName+"  with "+l);            }        //@AfterReturning("execution(int AOP.Calculator.*(int, int))",returning="result")    public void log3(JoinPoint jp,Object result){        String methodName = jp.getSignature().getName();                System.out.println("Method name: "+methodName+" end with "+result);    }               public void log4(JoinPoint jp,Exception e){
//注意这里填写什么异常就捕获什么异常,比如NullPointExcepiton只捕获空指针异常。 String methodName = jp.getSignature().getName(); List l = Arrays.asList(jp.getArgs()); System.out.println("Method name: "+methodName+" with "+l+" error:"+e); } public Object log5(ProceedingJoinPoint pjp){ Object result =null; try { System.out.println("Around 前置"); //前置 result = pjp.proceed(); System.out.println("Around 后置");//后置 } catch (Throwable e) {
//异常 // TODO Auto-generated catch block System.out.println("Around 异常"); e.printStackTrace(); } System.out.println("Around 返回");//返回 return result; }}

bean 修改如下

 

转载于:https://www.cnblogs.com/s1127736971/p/7521875.html

你可能感兴趣的文章
python 迭代
查看>>
File查看目录
查看>>
去除ActionBar的方法
查看>>
STM8S——Universal asynchronous receiver transmitter (UART)
查看>>
Flink - state管理
查看>>
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
查看>>
ArcGIS JS Demo
查看>>
webservice发布问题,部署iis后调用不成功
查看>>
Koch 分形,海岸线,雪花
查看>>
ubuntu系统下Python虚拟环境的安装和使用
查看>>
IOS7开发~新UI学起(二)
查看>>
软件过程度量和CMMI模型概述
查看>>
数据结构(DataStructure)与算法(Algorithm)、STL应用
查看>>
Linux与Windows xp操作系统启动过程
查看>>
linux运维、架构之路-Kubernetes1.13离线集群部署双向认证
查看>>
[Leetcode]Substring with Concatenation of All Words
查看>>
Gem install rmagick 报错问题~
查看>>
验证一个方法触发时机
查看>>
25句充满正能量的句子
查看>>
python学习手册笔记——27.更多实例
查看>>