zoukankan      html  css  js  c++  java
  • aspect 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();
        }
    复制代码
  • 相关阅读:
    03.《架构漫谈》阅读笔记
    02.《架构漫谈》阅读笔记
    03.《架构之美》阅读笔记
    02.《架构之美》阅读笔记
    01.《架构之美》阅读笔记
    软件架构中的质量属性--以淘宝网为例(小论文)
    MVC框架介绍分析
    论面向服务架构设计及其应用
    1.26学习进度总结
    1.24学习进度总结
  • 原文地址:https://www.cnblogs.com/s1297-lgy/p/7398800.html
Copyright © 2011-2022 走看看