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)
公告
昵称: 园龄: 粉丝: 关注:
+加关注
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
30 | 31 | 2 | 3 | 4 | 5 | ||||
6 | 7 | 8 | 9 | 10 | 11 | 12 | |||
13 | 14 | 15 | 16 | 17 | 18 | 19 | |||
20 | 21 | 22 | 23 | 24 | 25 | 26 | |||
27 | 28 | 29 | 30 | 31 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
昵称:
退出 订阅评论
[Ctrl+Enter快捷键提交]