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

    声明式增强

    IdoSomeService接口

    public interface IdoSomeService {
        public void doSome();
    }

    IdoSomeServiceImpl实现类

    /*原始对象*/
    public class IdoSomeServiceImpl implements IdoSomeService{
        public void doSome(){
            System.out.println("=========真实业务===========");
        }
    }

    MyBeforeAdvice切面类

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

    applicationContext.xml配置

     <!--注入业务Bean-->
        <bean id="idoSomeService" class="com.spring.proxyfactory.IdoSomeServiceImpl"></bean>
        <!--增强:切面-->
        <bean id="myBeforeAdvice" class="com.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>
            <!--更换代理方式  proxyTargetClass默认值为false  默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
            <property name="proxyTargetClass" value="true"></property>
        </bean>

    测试类

    public class ProxyFactoryTest {
        public static void main(String[] args) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂Bean
            IdoSomeService idoSomeServiceProxy = (IdoSomeService)ctx.getBean("proxyFactory");
            idoSomeServiceProxy.doSome();
        }
    }

    环绕增强 

    IdoSomeService接口

    public interface IdoSomeService {
        public void doSome();
    }
    IdoSomeServiceImpl实现类
    /*原始对象*/
    public class IdoSomeServiceImpl implements IdoSomeService {
        public void doSome(){
            System.out.println("=========真实业务===========");
        }
    }
    MyAroundAdvice增强类
    /*环绕增强*/
    public class MyAroundAdvice implements MethodInterceptor{
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            System.out.println("============环绕前=============");
            //调用核心业务方法      也可以获取方法内的参数   也可以获取目标对象
            Object proceed = invocation.proceed();
            Object aThis = invocation.getThis();
            System.out.println(aThis);
            System.out.println("============环绕后=============");
            return proceed;
        }
    }

    测试类

    public class ProxyFactoryTest {
        public static void main(String[] args) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂Bean
            IdoSomeService idoSomeServiceProxy = (IdoSomeService)ctx.getBean("proxyFactory");
            idoSomeServiceProxy.doSome();
        }
    }
    异常增强
    接口IdoSomeService
    public interface IdoSomeService {
        public void doSome() throws Exception;
    }
    IdoSomeServiceImpl实现类
    /*原始对象*/
    public class IdoSomeServiceImpl implements IdoSomeService {
        public void doSome() throws Exception{
            int result=5/0;
            System.out.println("=========真实业务===========");
        }
    }
    MyAdvice类
    public class MyAdvice {
        public void afterThrowing(Exception ex){
            System.out.println("=====发生了异常,执行增强操作===============");
        }
    
    

    applicationContext.xml配置

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

    测试类
    public class AroundTest {
        public static void main(String[] args){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂Bean
            IdoSomeService idoSomeServiceProxy = (IdoSomeService) ctx.getBean("idoSomeService");
            
            try {
                idoSomeServiceProxy.doSome();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            System.out.println("1231231231131312123");
        }
    }
    
    

    最终增强

    接口IdoSomeService
    public interface IdoSomeService {
        public void doSome() throws Exception;
    }
    IdoSomeServiceImpl实现类
    /*原始对象*/
    public class IdoSomeServiceImpl implements IdoSomeService {
        public void doSome() throws Exception{
            int result=5/0;
            System.out.println("=========真实业务===========");
        }
    }
    MyAdvice类
    public void afterAdvice(){
    System.out.println("======执行最终异常===============");
    }
    
    

    applicationContext.xml配置

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

    测试类
    public class AroundTest {
        public static void main(String[] args){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取代理工厂Bean
            IdoSomeService idoSomeServiceProxy = (IdoSomeService) ctx.getBean("idoSomeService");
            
            try {
                idoSomeServiceProxy.doSome();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            System.out.println("1231231231131312123");
        }
    }


  • 相关阅读:
    级数求和
    c++版a+b问题的各种无聊做法
    2017 Multi-University Training Contest
    2017 Multi-University Training Contest
    [DP] UVA-1626 Brackets sequence
    Codeforces Round #426 (Div. 2) [A、B、C]
    一只弱菜的博客之旅
    关于数据库保存的二进制图片无法在colorbox插件中显示的解决办法
    windows+caffe+vs2013+cuda6.5配置记录
    Ubuntu14.04+cuda6.5+opencv2.4.9+MATLAB2013a+caffe配置记录(五)——安装Caffe
  • 原文地址:https://www.cnblogs.com/Chencheno/p/11759486.html
Copyright © 2011-2022 走看看