Spring 通知和顧問進行增強
阿新 • • 發佈:2018-11-24
sele let tex mar oca bsp version readv cep
<property name="8" value="顧問id">
</bean>
使用顧問增加前置增強和後置增強
<bean id="1" class="目標對象"></bean>
<bean id="2" class="代理對象"></bean>
<bean id="3" class="顧問">
<property name="5" ref="代理對象id">
<property name="6" value="匹配的規則">
</bean>
<bean id="4" class="代理工廠Bean">
<property name="7" ref="目標對象">
<property name="8" value="顧問id">
</bean>
前置增強:
XML代碼:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--實現類(目標對象)--> <bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean> <!--增強類(代理對象)--> <bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean> <!--顧問--> <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="MMBA"></property><!--傳的是代理對象--> <property name="mappedNames" value="*s*"></property><!--匹配的方法--> </bean> <!--代理工廠Bean--> <bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="SSI"></property><!--接收目標對象--> <property name="interceptorNames" value="advisor"></property><!--然後把顧問拿進來--> </bean> </beans>
測試方法代碼:
import cn.Spring.Day16Advisor.IServiceSql; import cn.Spring.Day16Advisor.ServiceSqlImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test20181124 { @Test public void test1(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationAdvisor.xml"); IServiceSql bean =(IServiceSql) applicationContext.getBean("MyProxy");//傳入代理工廠Bean的id bean.delete(); bean.insert(); bean.select(); bean.update(); } }
代理類:
前置加後置增強:
XML代碼:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--實現類(目標對象)--> <bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean> <!--增強類(代理對象)--> <bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean> <!--顧問--> <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="MMBA"></property><!--傳的是代理對象--> <property name="mappedNames" value="*s*"></property><!--匹配的方法--> </bean> <!--增強類(代理對象)--> <bean id="MARA" class="cn.Spring.Day16Advisor.MyAfterReturningAdvice"></bean> <!--顧問--> <bean id="advisorT" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="MARA"></property><!--傳的是代理對象--> <property name="mappedNames" value="*s*"></property><!--匹配的方法--> </bean> <!--代理工廠Bean--> <bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="SSI"></property><!--接收目標對象--> <property name="interceptorNames" value="advisor,advisorT"></property><!--然後把顧問拿進來--> </bean> </beans>
需要註意的是在代理工廠Bean中interceptorNames的value值要多個的話使用逗號隔開。
結果:
Spring 通知和顧問進行增強