Spring Aspectj 代理 前置 後置 以及 異常
阿新 • • 發佈:2018-11-27
- public interface IDog2 {
- public void dog();
- public String run();
- }
- public class Dog2 implements
- public void dog(){
- System.out.println("===測試顧問==1dog()");
- }
- public
- int resul=5/0;
- System.out.println("====測試顧問===2run()");
- return "add";
- }
- }
- @Aspect
- public class MyAspectj {
- //前置增強
- //@Before(value = "execution(* *..springAspectj.*.*(..))")
- public void MyBefore(){
- System.out.println("====這是前置增強====");
- }
- //後置增強
- // @AfterReturning(value = "execution(* *..springAspectj.*.*(..))")
- public void After(){
- System.out.println("====這是後置增強====");
- }
- //環繞增強 相對於 後置增強 可以 改變 返回值的 內容
- // @Around(value = "execution(* *..springAspectj.*.*(..))")
- public Object around(ProceedingJoinPoint point) throws Throwable {
- System.out.println("==環繞前===");
- Object result = point.proceed();
- System.out.println("==環繞之後===");
- if (result!=null){
- String stu=(String)result;
- return stu.toUpperCase();
- }else {
- return null;
- }
- }
- @AfterThrowing(value = "execution(* *..springAspectj.*.*(..))")
- public void After2(){
- System.out.println("====這是異常====");
- }
- }
- //Aspectj 代理
- //測試 前置 後置 增強
- //測試 環繞增強 可以改變 返回值
- //測試 異常
- @Test
- public void aVoid3(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext09Aspectj.xml");
- IDog2 dog =(IDog2) ctx.getBean("dog2");
- dog.dog();
- System.out.println();
- String run = dog.run();
- System.out.println(run);
- }