码迷,mamicode.com
首页 > Web开发 > 详细

aspect xml

时间:2017-08-20 10:16:51      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:cee   方法   +++   throw   cas   ade   uri   pat   XML   

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文件

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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
">
    <!--01.目标对象-->
    <bean id="someService" class="cn.bdqn.spring18.SomeService"></bean>

    <!--02.增强类-->
    <bean id="aspect" class="cn.bdqn.spring18.MyAspect"></bean>

    <!--aop-->
    <aop:config>
        <!--设置一个切点-->
        <aop:pointcut id="mycut" expression="execution(* *..spring18.*.*(..))"></aop:pointcut>
        <aop:aspect ref="aspect">
            <aop:before method="before" pointcut-ref="mycut"></aop:before>
            <aop:after-returning method="afterReturing" pointcut-ref="mycut"></aop:after-returning>
            <aop:after-returning method="afterReturing(java.lang.String)" returning="result" pointcut-ref="mycut"></aop:after-returning>
            <aop:around method="around" pointcut-ref="mycut"></aop:around>
            <aop:after-throwing method="afterThrowing" pointcut-ref="mycut"></aop:after-throwing>
           <aop:after-throwing method="afterThrowing(java.lang.Exception)" throwing="ex" pointcut-ref="mycut"></aop:after-throwing>
            <aop:after method="after" pointcut-ref="mycut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>
技术分享

3.测试类

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

aspect xml

标签:cee   方法   +++   throw   cas   ade   uri   pat   XML   

原文地址:http://www.cnblogs.com/s1297-lgy/p/7398800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!