zoukankan      html  css  js  c++  java
  • 多种方式实现AOP

    一、使用代理工厂完成声明式增强

    1.创建业务接口

    public interface IdoSomeService {
        public void doSomething();
    }

    2.创建接口实现类

    public class IdoSomeServiceImpl implements  IdoSomeService{
        @Override
        public void doSomething() {
            System.out.println("真实业务");
        }
    }

    3.创建切面类

    /**
     * 切面
     */
    public class MyBeforeAdvice implements MethodBeforeAdvice {
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("前置增强");
        }
    }

    4.编写applicationContext.xml配置文件

    <!--注入业务Bean-->
        <bean id="idoSomeService" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
        <!--增强:切面-->
        <bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean>
        <!--使用代理工厂实现增强-->
        <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--将增强和业务织入 到一起 -->
            <property name="target" ref="idoSomeService"></property>
            <!--拦截增强类-->
            <property name="interceptorNames" value="myBeforeAdvice"></property>
         </bean>

    5.创建测试类

        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂
            IdoSomeService  idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
            idoSomeService.doSomething();
        }
    }

    二、使用代理工厂完成环绕增强

    1.创建业务接口

    public interface IdoSomeService {
        public void doSomething();
    }

    2.创建业务接口实现类

    public class IdoSomeServiceImpl implements  IdoSomeService{
        @Override
        public void doSomething() {
            System.out.println("真实业务");
        }
    }

    3.创建切面类

    public class MyAroundAdvice implements MethodInterceptor {
        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            System.out.println("环绕前");
    
            //调用核心业务方法也可以获取方法内的参数 也可以获取目标对象
            Object proceed = methodInvocation.proceed();
            Object aThis = methodInvocation.getThis();
            System.out.println(aThis);
    
            System.out.println("环绕后");
            return proceed;
        }
    }

    4.编写applicationContext.xml配置文件

        环绕增强
        <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
        <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
        <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--将增强和业务织入到一起-->
            <property name="target" ref="idoSomeService"></property>
            <property name="interceptorNames" value="myAroundAdvice"></property>
            <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
            <property name="proxyTargetClass" value="true"></property>
         </bean>

    4.创建测试类

    public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂
            IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
            idoSomeService.doSomething();
        }

    三、使用工厂代理工厂完成异常增强

    1.创建业务接口

    public interface IdoSomeService {
        public void doSomething() throws Exception;
    }

    2.创建业务接口实现类

    public class IdoSomeServiceImpl implements IdoSomeService {
        @Override
        public void doSomething() throws Exception{
            int result=5/0;
            System.out.println("真实业务");
        }
    }

    3.创建切面类

    public class MyThrowAdvice{
       public void afterThrowing(Exception ex){
           System.out.println("=====发生了异常,执行增强操作===============");
       }
    }

    4.编写applicationContext.xml配置文件

    <!--环绕增强-->
        <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
        <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
        <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--将增强和业务织入到一起-->
            <property name="target" ref="idoSomeService"></property>
            <property name="interceptorNames" value="myAroundAdvice"></property>
            <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
            <property name="proxyTargetClass" value="true"></property>
         </bean>

    5.创建测试类

     public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂
            IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
            try{
                idoSomeService.doSomething();
    
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("2222222222222");
        }

    四、使用代理工厂实现最终增强

    1.创建业务接口

    public interface IdoSomeService {
        public void doSomething() throws Exception;
    }

    2.创建业务接口实现类

    public class IdoSomeServiceImpl implements IdoSomeService {
        @Override
        public void doSomething() throws Exception{
            int result=5/0;
            System.out.println("真实业务");
        }
    }

    3.创建切面类

    public class MyThrowAdvice{
        public void afterAdvice(){
            System.out.println("======执行最终异常===============");
        }
    }


    4.编写applicationContext.xml配置文件

    <bean id="idoSomeService" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
        <bean id="myAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
        <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
            <aop:aspect ref="myAdvice">
                <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
                <aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
            </aop:aspect>
        </aop:config>

    5.创建测试类

    public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂
            IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
            try{
                idoSomeService.doSomething();
    
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("2222222222222");
        }
  • 相关阅读:
    【TJOI2019 Day2】简要题解
    【TJOI2019 Day2】简要题解
    【Codeforces 750G】—New Year and Binary Tree Paths(数位dp)
    【Codeforces 750G】—New Year and Binary Tree Paths(数位dp)
    【TJOI2019 Day1】简要题解
    【TJOI2019 Day1】简要题解
    【LOJ #6503】【雅礼集训 2018 Day4】—Magic(生成函数+分治NTT)
    【LOJ #6503】【雅礼集训 2018 Day4】—Magic(生成函数+分治NTT)
    【HihoCoder #1529】— 不上升序列(dp+斜率)
    【HihoCoder #1529】— 不上升序列(dp+斜率)
  • 原文地址:https://www.cnblogs.com/szhhhh/p/11758746.html
Copyright © 2011-2022 走看看