引入和其它类型的spring通知有所不同.其它类型的通知是在方法调用的周围织入到不同的连接点.而引入则是影响整个类.
引入让你能动态地建立复合对象.
spring 通过一个特殊的方法拦截接口IntroductionInterceptor
要引入的接口
现在我们实现一个introductionInterceptor
spring 为我们提供一个方便类的处理大多数的应用.DelegatingIntroductInterceptor
如果你想实现方法的拦截调用invoke方法
下面是配制
引入让你能动态地建立复合对象.
spring 通过一个特殊的方法拦截接口IntroductionInterceptor
java 代码
- boolean implementsInterface(Class intf);
- 这个方法判断是否是引入的接口
java 代码
- package aop.introduction;
- public interface IChange {
- public boolean ichange();
- }
java 代码
- package aop.introduction;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.IntroductionInterceptor;
- public class IChangeAdvisora implements IntroductionInterceptor,IChange{
- public Object invoke(MethodInvocation m) throws Throwable {
- if(implementsInterface(m.getMethod().getDeclaringClass())){
- return m.getMethod().invoke(this, m.getArguments());
- //通过MethodInvocation 和invoke代理要引入的方法
- }else{
- return m.proceed(); //委托其它方法,不是引入,就像那个环绕通知
- }
- }
- public boolean implementsInterface(Class intf) {
- return intf.isAssignableFrom(IChange.class);
- }
- public boolean ichange() {
- System.out.println("intrduction");
- return false;
- }
- }
java 代码
- package aop.introduction;
- import org.springframework.aop.support.DelegatingIntroductionInterceptor;
- public class IChangeAdvisor extends DelegatingIntroductionInterceptor
- implements IChange {
- public boolean ichange() {
- // TODO 自动生成方法存根
- System.out.println("ichage");
- return false;
- }
- } //比起上面那个类算得比较简洁,DelegatingIntroductionInterceptor 替我们实现invoke()
java 代码
- package aop.introduction;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.support.DelegatingIntroductionInterceptor;
- public class IChangeAdvisorb extends DelegatingIntroductionInterceptor implements IChange {
- public boolean ichange() {
- // TODO 自动生成方法存根
- return false;
- }
- public Object invoke(MethodInvocation e) throws Throwable{
- String name = e.getMethod().getName();
- if(name.indexOf("set") == 0){ //这里拦截set方法
- throw new IllegalMonitorStateException();
- }
- return super.invoke(e);
- }
- }
java 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="targetbean" class="aop.introduction.TagetBean" />
- <bean id="iChangeAdvisor" class="aop.introduction.IChangeAdvisorb" />
- //这里配制引入advisor,看你用得是那个
- <bean id="advisor"
- class="org.springframework.aop.support.DefaultIntroductionAdvisor">
- <constructor-arg>
- <ref bean="iChangeAdvisor" />
- </constructor-arg>
- </bean>
- <bean id="bean"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target">
- <ref bean="targetbean" />
- </property>
- <property name="interceptorNames">
- <list>
- <value>advisor</value>
- </list>
- </property>
- <property name="proxyInterfaces">
- <value>aop.introduction.IChange</value>
- </property>
- <property name="proxyTargetClass">
- <value>true</value>
- </property>
- </bean>
- </beans>
java 代码
- package aop.introduction;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- public class IntroductionTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new FileSystemXmlApplicationContext(
- "./src/applicationContext.xml");
- TagetBean bean = (TagetBean) ctx.getBean("bean");
- // ((IChange) ctx.getBean("bean")).ichange();
- IChange mod = (IChange) bean;
- // test interfaces
- System.out.println("Is TargetBean?: " + (bean instanceof TagetBean));
- System.out.println("Is IsModified?: " + (bean instanceof IChange));
- // test is modified implementation
- // mod.ichange();
- // bean.setName("ddd");
- System.out.println("Has been modified?: " + mod.ichange());
- }
- }