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();
        }
  • 相关阅读:
    终于看到费德勒在法网如愿!
    o(∩_∩)o...,今天去博客园了!
    条款4:使用Conditional特性代替#if条件编译
    MSDTC无法启动的解决方法
    2009 很有意义的一天
    从现在开始,争取记录每天所学到的、所感受到的、所遇见到的点点滴滴!
    了解MOSS2007 内容类型ID(Content Type IDs)命名规则
    CreateSpecificCulture('zhcn')和new CultureInfo('zhcn')的区别
    金华大显数码科技有限公司诚聘
    使用SQL Server中按位于来表示组合状态
  • 原文地址:https://www.cnblogs.com/shiwz/p/7267900.html
Copyright © 2011-2022 走看看