顾问。

public interface ISomeService {
    public void doSome();
    public void dade();
}
复制代码
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("++++++++++++++de+++++++++++++");
    }
}
复制代码

 

1.名称匹配方法顾问

配置文件

复制代码
<?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.spring13.SomeService"></bean>
    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.bdqn.spring13.MyBeforeAdvise"></bean>
    <!--02.增强 :顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice" ></property>
        <property name="mappedNames" value="doSome"></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.正则 顾问

复制代码
<?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.spring14.SomeService"></bean>
    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.bdqn.spring14.MyBeforeAdvise"></bean>
    <!--02.增强 :顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice" ></property>
        <property name="pattern" value=".*b.*"></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>
复制代码

单侧

复制代码
 /*名称匹配方法顾问*/
    @Test
    public void test08(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring11.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
        service.dade();
    }
复制代码
复制代码
 /*正则   顾问*/
    @Test
    public void test09(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring12.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
        service.dade();
    }
复制代码

 

posted @ 2017-07-31 16:57  执念哈i  阅读(126)  评论(0编辑  收藏  举报