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

    public interface ISomeService {
        public void doSome();
        public void dade();
    }
    public interface ISomeService1 {
        public void doSome();
        public void dade();
    }
    import org.springframework.aop.AfterReturningAdvice;
    import java.lang.reflect.Method;
    
    public class MyAfterReturningAdvice implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("===========after================");
        }
    }
    import org.springframework.aop.MethodBeforeAdvice;
    import java.lang.reflect.Method;
    
    //前置通知
    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("我们都要找到Java开发工作,薪资6,7,8,9,10K");
        }
    
        public void dade() {
            System.out.println("==============add============");
        }
    
    
    }
    public class SomeService1 implements ISomeService1 {
        //核心业务
        public void doSome(){
            System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
        }
    
        public void dade() {
            System.out.println("==============add============");
        }
    
    
    }

    1.Advisor自动代理生成器

    配置文件

    <?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.spring15.SomeService"></bean>
        <bean id="someService1" class="cn.bdqn.spring15.SomeService1"></bean>
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.bdqn.spring15.MyBeforeAdvise"></bean>
        <bean id="afterAdvice" class="cn.bdqn.spring15.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>

    2.BeanName自动代理生成器

    配置文件

    <?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.spring16.SomeService"></bean>
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.bdqn.spring16.MyBeforeAdvise"></bean>
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames" value="someService"></property>
            <property name="interceptorNames" value="beforeAdvice"></property>
        </bean>
    </beans>

    3.单侧

    /*Advisor自动代理生成器*/
        @Test
        public void test(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring13.xml");
            ISomeService service = (ISomeService) ctx.getBean("someService");
            ISomeService1 service1 = (ISomeService1) ctx.getBean("someService1");
            service.doSome();
            service.dade();
            service1.doSome();
            service1.dade();
        }
      // BeanName自动代理生成器
        @Test
        public void test10(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring14.xml");
            ISomeService service = (ISomeService) ctx.getBean("someService");
            service.doSome();
            service.dade();
        }
  • 相关阅读:
    Qt Creator编译问题
    『重构--改善既有代码的设计』读书笔记----Move Method
    『重构--改善既有代码的设计』读书笔记----Substitute Algorithm
    『重构--改善既有代码的设计』读书笔记----Replace Method with Method Object
    『重构--改善既有代码的设计』读书笔记----Remove Assignments to Parameters
    『重构--改善既有代码的设计』读书笔记----Split Temporary Variable
    Qt信号槽连接在有默认形参下的情况思考
    巧用Session Manager还原Firefox丢失会话
    『重构--改善既有代码的设计』读书笔记----代码坏味道【5】
    『重构--改善既有代码的设计』读书笔记----代码坏味道【4】
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7263662.html
Copyright © 2011-2022 走看看