Spring Aspectj 代理 前置 后置 以及 异常
其他
2018-11-27 02:30:02
阅读次数: 0
- public interface IDog2 {
- public void dog();
-
- public String run();
- }
- public class Dog2 implements IDog2 {
- public void dog(){
- System.out.println("===测试顾问==1dog()");
- }
-
- public String run() {
- int resul=5/0;
- System.out.println("====测试顾问===2run()");
- return "add";
- }
- }
- @Aspect
- public class MyAspectj {
-
-
- public void MyBefore(){
- System.out.println("====这是前置增强====");
- }
-
-
- public void After(){
- System.out.println("====这是后置增强====");
- }
-
-
-
-
- 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("====这是异常====");
- }
- }
-
-
-
-
- @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);
- }
转载自blog.csdn.net/qq_36074218/article/details/76674991