zoukankan      html  css  js  c++  java
  • 自动代理生成器

    1.默认Advisor自动代理生成器

    接口:ISomeService

    public interface ISomeService {
        public void doSome();
    
        public void doSecont();
    }

    public class MyAfterReturningAdvice implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("-+-======================after=================-+-");
        }
    }
    public class MyBeforeAdvise implements MethodBeforeAdvice {
    
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("==========log==========");
    
        }
    }
    public class SomeService implements ISomeService {
        //核心业务
        public void doSome() {
            System.out.println("拜托别让他一番努力换来是奢求!");
        }
    
        public void doSecont() {
            System.out.println("++===================Secont====================++");
    
        }
    }

    配置文件

    <?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
    ">
        <!--默认Advisor自动代理生成器-->
        <!--01.目标对象-->
        <bean id="someService" class="cn.happy.spring10aop_defaultZidonAgent.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.happy.spring10aop_defaultZidonAgent.MyBeforeAdvise"></bean>
        <bean id="afterAdvice" class="cn.happy.spring10aop_defaultZidonAgent.MyAfterReturningAdvice"></bean>
        <!--增强:前置顾问-->
        <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice" ref="beforeAdvice"></property>
            <property name="pattern" value=".*do.*"></property>
        </bean>
    
        <!--增强:后置顾问-->
        <bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice" ref="afterAdvice"></property>
            <property name="pattern" value=".*do.*"></property>
        </bean>
    
        <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
    
        </beans>

    单测

     //1.默认Advisor自动代理生成器
        @Test
        public void test03(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext07_aop07_defaultzidonAgent.xml");
            ISomeService service = (ISomeService) ctx.getBean("someService");
            service.doSome();
            service.doSecont();
        }

    2.BeanName自动代理生成器

    接口:ISomeService

    public interface ISomeService {
        public void doSome();
    
        public void doSecont();
    }

    public class MyBeforeAdvise implements MethodBeforeAdvice {
    
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("==========log==========");
    
        }
    }
    public class SomeService implements ISomeService {
        //核心业务
        public void doSome() {
            System.out.println("拜托别让他一番努力换来是奢求!");
        }
    
        public void doSecont() {
            System.out.println("++===================Secont====================++");
    
        }
    }

    配置文件

    <?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
    ">
        <!--BeanName自动代理生成器-->
        <!--01.目标对象-->
        <bean id="someService" class="cn.happy.spring11aop_BeanNameZidonAgent.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.happy.spring11aop_BeanNameZidonAgent.MyBeforeAdvise"></bean>
    
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <!--目标对象-->
            <property name="beanNames" value="someService"></property>
            <property name="interceptorNames" value="beforeAdvice"></property>
        </bean>
    </beans>

    单测

    //2.BeanName自动代理生成器
        @Test
        public void test04(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext08_aop08_BeanNamezidonAgent.xml");
            ISomeService service = (ISomeService) ctx.getBean("someService");
            service.doSome();
            service.doSecont();
        }
  • 相关阅读:
    PetaPOCO 一对多 多对一 多对多
    PetaPoco使用要点
    MySQL_杭州北仓 12.3-12.7需求活动期间累计下单达到3天及以上的客户_20161212
    Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207
    Python 2.7_First_try_爬取阳光电影网_20161206
    MySQL计算销售员昨日各指标综合得分_20161206
    MySQL_关于用嵌套表计算的可以不用 20161205
    MySQL_财务统计各产品品类各城市上周收入毛利表_20161202
    借助取色工具ColorPix对Pycharm编辑器设定自己喜欢的代码颜色_20161202
    python2.7 爬虫_爬取小说盗墓笔记章节及URL并导入MySQL数据库_20161201
  • 原文地址:https://www.cnblogs.com/shiwz/p/7267934.html
Copyright © 2011-2022 走看看