aspectj xml

2021-06-11 02:05

阅读:527

标签:ref   span   ora   +++   aspect   instance   image   1.2   对象   

1.接口和类

技术分享

1.1 ISomeService 接口

public interface ISomeService {

    public void doSome();

    public void dade();
}

1.2  SomeService类

public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void dade() {
        //异常
        //int sun=5/0;
        System.out.println("++++++++++++++de+++++++++++++");
    }

}

1.3  MyAspect 类

public class MyAspect {

    //前置增强
    public void before(){
        System.out.println("=====before========");
    }
    //后置增强
    public void afterReturing(){
        System.out.println("=====after========");
    }
    //后置增强   目标方法的返回值
    public void afterReturing(String result){
        System.out.println("=====后置通知方法 result========"+result);
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("目标方法执行前执行");
        Object result = pjp.proceed();
        System.out.println("目标方法执行后执行");
        String temp=null;
        if (result!=null) {
            temp = (String) result;
            temp=temp.toUpperCase();
        }
        return temp;
    }
    //异常通知
    public void afterThrowing(){
        System.out.println("异常通知");
    }
    //异常通知
    public void afterThrowing(Exception ex){
        System.out.println("异常通知 ex="+ex.getMessage());
    }

    //最终通知
    public void after(){
        System.out.println("最终通知");
    }

}

2.xml文件

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
">
    
    "someService" class="cn.bdqn.spring18.SomeService">"aspect" class="cn.bdqn.spring18.MyAspect">"mycut" expression="execution(* *..spring18.*.*(..))">
        ref="aspect">
            "before" pointcut-ref="mycut">
            "afterReturing" pointcut-ref="mycut">
            "afterReturing(java.lang.String)" returning="result" pointcut-ref="mycut">
            "around" pointcut-ref="mycut">
            "afterThrowing" pointcut-ref="mycut">
           "afterThrowing(java.lang.Exception)" throwing="ex" pointcut-ref="mycut">
            "after" pointcut-ref="mycut">
        
    

3.测试类

 //aspectj xml
    @Test
    public void test20(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring16.xml");
        ISomeService service = (ISomeService) ctx.getBean("someService");
        service.doSome();
        service.dade();
    }

 

aspectj xml

标签:ref   span   ora   +++   aspect   instance   image   1.2   对象   

原文地址:http://www.cnblogs.com/qjt970518--/p/7293925.html


评论


亲,登录后才可以留言!