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();
    }

 

posted @ 2017-08-01 12:42  <烟花易冷>  阅读(208)  评论(0编辑  收藏  举报