zoukankan      html  css  js  c++  java
  • Spring 顾问

    1.名称匹配方法切入点顾问

    接口: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: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">
    
        <!--前置通知 名称匹配方法切入点顾问-->
        <!--01.目标对象-->
        <bean id="someService" class="cn.happy.spring08aop_Advice.SomeService"></bean>
        <!--02.前置增强(通知)-->
        <bean id="beforeAdvice" class="cn.happy.spring08aop_Advice.MyBeforeAdvise"></bean>
    
        <!--02.前置增强(顾问)-->
        <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
            <property name="advice" ref="beforeAdvice"></property>
            <property name="mappedNames" value="beforeAdvisor"></property>
        </bean>
        <!--03.aop-->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
    
            <property name="interceptorNames" value="beforeAdvice"></property>
            <property name="proxyTargetClass" value="true"></property>
        </bean>
    </beans>

    单测

    //1.前置通知 名称匹配方法切入点顾问
        @Test
        public void test01(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext05_aop05_Advice.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
            service.doSecont();
        }

    2.正则表达式 匹配方法切入点顾问

    接口: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: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">
    
        <!--正则表达式 匹配方法切入点顾问-->
        <!--01.目标对象-->
        <bean id="someService" class="cn.happy.spring09aop_AdviceZenz.SomeService"></bean>
        <!--02.前置增强(通知)-->
        <bean id="beforeAdvice" class="cn.happy.spring09aop_AdviceZenz.MyBeforeAdvise"></bean>
    
        <!--02.前置增强(顾问)-->
        <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice" ref="beforeAdvice"></property>
            <property name="pattern" value=".*d.*"></property>
        </bean>
        <!--03.aop-->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
    
            <property name="interceptorNames" value="beforeAdvisor"></property>
    
        </bean>
    </beans>

    单测

     //2.正则表达式 匹配方法切入点顾问
        @Test
        public void test02(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext06_aop06_Zenz.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
            service.doSecont();
        }
  • 相关阅读:
    实现FTP断点续传
    系统软件自动部署实现方案
    QT实现多语言切换
    QTreeWidget实现动态加载本地文件系统
    QuaZip实现多文件打包
    FileZilla命令行实现文件上传以及CreateProcess实现静默调用
    ctkPlugin插件系统实现项目插件式开发
    Windows与Linux下文件操作监控的实现
    QT皮肤系统的动态切换
    OpenGL 学习
  • 原文地址:https://www.cnblogs.com/shiwz/p/7267900.html
Copyright © 2011-2022 走看看