博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring增强
阅读量:5037 次
发布时间:2019-06-12

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

1.前置增强

接口:ISomeService

public interface ISomeService {    public void doSome();}

public class MyBeforeAdvise implements MethodBeforeAdvice {    public void before(Method method, Object[] objects, Object o) throws Throwable {        System.out.println("==========log==========");    }}
public class SomeService implements ISomeService{    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }    }

配置文件

单测

//前置增强    @Test    public void test05(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");        SomeService service = (SomeService) ctx.getBean("proxyService");        service.doSome();    }

2.后置增强

接口:ISomeService 

public interface ISomeService {    public void doSome();}

public class MyAfterReturningAdvice implements AfterReturningAdvice {    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {        System.out.println("==========after==========");    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }}

配置文件

单测

//后置增强    @Test    public void test06(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

3.环绕增强

接口:ISomeService 

public interface ISomeService {    public void doSome();}

public class MyMethodInterceptor implements MethodInterceptor {    public Object invoke(MethodInvocation methodInvocation) throws Throwable {        System.out.println("before");        methodInvocation.proceed();        System.out.println("after");        return null;    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }}

 配置文件

单测

//环绕增强    @Test    public void test07(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

4.异常增强

接口:ISomeService   Mystoo

public interface ISomeService {    public void doSome();}
public interface Mystoo {    public void run();    public void run(String style);}

public class MystooImpl implements Mystoo {    public void run() {    }    public void run(String style) {    }}
public class MyThrowsAdvice implements ThrowsAdvice {    public void afterThrowing(Exception ex){        int age=6/0;        System.out.println("错误");    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }    public void doSecont() {    }}

配置文件

单测

//异常增强    @Test    public void test08(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

 

1.前置增强

接口:ISomeService

public interface ISomeService {    public void doSome();}

public class MyBeforeAdvise implements MethodBeforeAdvice {    public void before(Method method, Object[] objects, Object o) throws Throwable {        System.out.println("==========log==========");    }}
public class SomeService implements ISomeService{    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }    }

配置文件

单测

//前置增强    @Test    public void test05(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");        SomeService service = (SomeService) ctx.getBean("proxyService");        service.doSome();    }

2.后置增强

接口:ISomeService 

public interface ISomeService {    public void doSome();}

public class MyAfterReturningAdvice implements AfterReturningAdvice {    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {        System.out.println("==========after==========");    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }}

配置文件

单测

//后置增强    @Test    public void test06(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

3.环绕增强

接口:ISomeService 

public interface ISomeService {    public void doSome();}

public class MyMethodInterceptor implements MethodInterceptor {    public Object invoke(MethodInvocation methodInvocation) throws Throwable {        System.out.println("before");        methodInvocation.proceed();        System.out.println("after");        return null;    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }}

 配置文件

单测

//环绕增强    @Test    public void test07(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

4.异常增强

接口:ISomeService   Mystoo

public interface ISomeService {    public void doSome();}
public interface Mystoo {    public void run();    public void run(String style);}

public class MystooImpl implements Mystoo {    public void run() {    }    public void run(String style) {    }}
public class MyThrowsAdvice implements ThrowsAdvice {    public void afterThrowing(Exception ex){        int age=6/0;        System.out.println("错误");    }}
public class SomeService implements ISomeService {    //核心业务    public void doSome() {        System.out.println("拜托别让他一番努力换来是奢求!");    }    public void doSecont() {    }}

配置文件

单测

//异常增强    @Test    public void test08(){        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");        ISomeService service = (ISomeService) ctx.getBean("proxyService");        service.doSome();    }

 

 
 
标签: 
好文要顶  关注我  收藏该文 
 
 
 
+加关注
0
0
 
 
 
上一篇:
下一篇:
posted @ 
2017-07-31 17:03   阅读(
3) 评论(
0)   
 
 
刷新评论
发表评论

昵称:

评论内容:
引用 
粗体 
链接 
缩进 
代码 
图片
 

 退出 订阅评论

 

[Ctrl+Enter快捷键提交]

 
 
最新IT新闻:
· 
· 
· 
· 
· 
» 
最新知识库文章:
· 
· 
· 
· 
· 
» 
 
 
 

转载于:https://www.cnblogs.com/wangdan123/p/7398862.html

你可能感兴趣的文章
robot framework 接口post请求需要加headers
查看>>
JDE隐藏Constant等(Hide Object)
查看>>
python3内置函数
查看>>
java学习之switch 等值判断
查看>>
hdu5036 Explosion 传递闭包
查看>>
WinXP下由于图标造成的System.Windows.Markup.XamlParseException
查看>>
js中ajax返回数据
查看>>
SQL execution plan
查看>>
maven settings.xml windows
查看>>
递归与二叉树_中序和后序重建二叉树
查看>>
C#winform的textbox怎么设置滚动条
查看>>
TensorFlow基础9——tensorboard显示网络结构
查看>>
HDU 1232 畅通工程 并查集
查看>>
linux下使用sftp【转】
查看>>
使用redis-cli定时执行指定命令
查看>>
Moresec
查看>>
《Effective Java》—— 创建与销毁对象
查看>>
UDP连接代码
查看>>
动态加载js文件
查看>>
MySql优化
查看>>