博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring(六)AOP切入方式
阅读量:6073 次
发布时间:2019-06-20

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

一、接口切入方式

实现类

package com.pb.entity;/** * 实体类 */public class Hello {    private String name;    private String password;        public void show(){        System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }

1.1、前置增强

 

package com.pb.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;/** * 前置增强的Bean * @author Administrator *实现MethodBeforeAdvice接口 */public class BeforeAdded implements MethodBeforeAdvice {    @Override    public void before(Method arg0, Object[] arg1, Object arg2)            throws Throwable {        System.out.println("====前置增强!=====");    }}

applicationContext.xml

 

1.2、后置增强

package com.pb.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/** * 后置增强 */public class AfterAdded implements AfterReturningAdvice {    @Override    public void afterReturning(Object arg0, Method arg1, Object[] arg2,            Object arg3) throws Throwable {        System.out.println("====这里是后置增强!====");    }}

 

 applicationContext.xml

 

1.3、异常增强

实体类中增加异常

package com.pb.entity;/** * 实体类 */public class Hello {    private String name;    private String password;        public void show(){        System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());        //加入异常        System.out.println(1/0);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }

 

package com.pb.aop;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;/** * 异常增强类 * @author Administrator * */public class ThrowingAdded implements ThrowsAdvice {    //第一种写法    public void afterThrowing(Exception ex){        System.out.println("我是异常增强!,,没处理异常,有问题就找我");            }    //第二种写法    public void afterThrowing(Method arg0, Object[] arg1, Object arg2,Exception ex){        System.out.println("我是异常增强!,,没处理异常,有问题就找我");            }}

 

 applicationContext.xml

1.4、以上综合

 

1.5、环绕增强

 

package com.pb.entity;/** * 实体类 */public class Hello {    private String name;    private String password;        public void show(){        System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());        //加入异常        System.out.println(1/0);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }

环绕增加Bean

package com.pb.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;/** * 环绕增强 * @author Administrator *实现接口MethodIntercepor */public class AroundAdded implements MethodInterceptor {    @Override    public Object invoke(MethodInvocation arg0) throws Throwable {        Object result=null;        try {            System.out.println("环绕增强开始!");             result=arg0.proceed();            System.out.println("环绕增强结束!");        } catch (Exception e) {            System.out.println("环绕增强异常!");        }finally{            System.out.println("环绕增强最终增强!");        }                return result;    }}

 

 applicationContext.xml

 

 

二、注解方式

 

package com.pb.entity;/** * 实体类 */public class Hello {    private String name;    private String password;        public void show(){        System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());        //加入异常        System.out.println(1/0);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }

aop的类

package com.pb.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;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;/** * 注解的方式现实AOP * @author Administrator * */@Aspectpublic class MyAnnotionAOP {        /*     * 前置     */    @Before(value="execution(* com.pb.entity.*.*(..))")    public void before(JoinPoint point){                System.out.println("前置增强");        System.out.println(point.getClass());    }    /*     * 后置     */    @AfterReturning(value="execution(* com.pb.entity.*.*(..))")    public void after(JoinPoint point){        System.out.println("后置增强");        //参数个数        System.out.println(point.getArgs().length);    }    /*     *异常      */    @AfterThrowing(value="execution(* com.pb.entity.*.*(..))")    public void afterThrowing(JoinPoint point){        System.out.println("我是异常增强");        System.out.println(point.getSignature().getName());    }    /**     * 环绕     */    @Around(value="execution(* com.pb.entity.*.*(..))")    public Object myAround(ProceedingJoinPoint point){        Object result=null;                try {            System.out.println("环绕增强开始了");            System.out.println(point.getKind()+point.getArgs());            point.proceed();            System.out.println("环绕增强后置增强了");            System.out.println(point.getTarget()+""+point.getClass());        } catch (Throwable e) {            System.out.println("环绕增强,异常增强处理");            e.printStackTrace();        }finally{            System.out.println("环绕增强最终增强");        }                return result;            }}

applicationContext.xml

三、Schema方式(推荐)

 Hello类同上

package com.pb.entity;/** * 实体类 */public class Hello {    private String name;    private String password;        public void show(){        System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());        //加入异常        System.out.println(1/0);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }

aop类

package com.pb.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;/** * SCHEMA方式切入类 *  * @author Administrator *  */public class MySchemaAOP {    /**     * 前置切入 可以有参数但是固定写法     */    public void before(JoinPoint point){        System.out.println("这里是前置增强切入");        System.out.println(point.getKind()+point.getArgs().toString());    }    /**     * 后置切入     */    public void after(JoinPoint point){        System.out.println("这里是后置增强切入");        System.out.println(point.getTarget()+point.getSignature().getName());    }    /**     * 异常切入     */    public void myException(JoinPoint point){        System.out.println("这里是异常增强切入");        System.out.println(point.getSourceLocation());    }    /**     * 环绕增强切入     */    public Object myAround(ProceedingJoinPoint point){        Object resut=null;        try {            System.out.println("环绕增强---前置增强");            resut=point.proceed();            System.out.println("环绕增强---后置增强");        } catch (Throwable e) {            System.out.println("环绕增强---异常增强");            e.printStackTrace();        }finally{            System.out.println("环绕增强---最终增强");        }        return resut;    }}

applicationContext.xml

 

转载地址:http://enngx.baihongyu.com/

你可能感兴趣的文章
反射操作公共成员变量
查看>>
小孩的linux
查看>>
CSS3 transforms 3D翻开
查看>>
java基础---->正则表达式
查看>>
2.2013/06/13_log(n)+1
查看>>
关于加载iframe时进度条不消失的问题
查看>>
poj 3984迷宫问题【广搜】
查看>>
oracle ORA-01840:输入值对于日期格式不够长
查看>>
python基础知识~logger模块
查看>>
SIP入门(二):建立SIPserver
查看>>
Servlet3.0的异步
查看>>
WebService连接postgresql( 失败尝试)
查看>>
从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?...
查看>>
Python-MacOSX下SIP引起的pip权限问题解决方案(非取消SIP机制)
查看>>
从MFQ方法到需求分析
查看>>
android.view.WindowManager$BadTokenException: Unable to add window
查看>>
HDU5012:Dice(bfs模板)
查看>>
iphone openssh
查看>>
Linux下MEncoder的编译
查看>>
Xamarin使用ListView开启分组视图Cell数据展示bug处理
查看>>