Spring顾问

来源:互联网 发布:linux驱动书籍 编辑:程序博客网 时间:2024/05/16 01:08
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();    }
原创粉丝点击