您的位置:首页 > 编程语言 > Java开发

SpringAOP中的IntroductionInterceptor

2016-07-21 00:19 573 查看
        Introduction(引入)是个特别的Advice,类通过实现AOP中的org.springframework.aop.IntroductionInterceptor在不改变原有方法的基础上却可以增加新的方法。IntroductionInterceptor继承了MethodInterceptor和DynamicIntroductionAdvice接口,其中implementsInterface()方法(继承自DynamicIntroductionAdvice)如果返回true,表示目前的
IntroductionInterceptor实现了给定的接口(也就是要额外增加行为的接口),此时要使用invoke()调用该接口上的方法,让目标执行额外的行为。需要注意的是不可能使用MethodInvocation的proceed()方法,因为要执行的是类原来没有的行为,proceed()方法没有意义。

示例:需要导入aopalliance-1.0.jar和spring-aop-2.5.1.jar

目标接口:

package com.yourcompany.spring;

public interface ISome {
public void doSome();
}
目标实现:

package com.yourcompany.spring;

public class Some implements ISome{
public void doSome(){
System.out.println("原来Some对象的功能...");
}
}
新增接口:

package com.yourcompany.spring;

public interface IOther{
public void doOther();
}
新增实现:

package com.yourcompany.spring;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

public class Other implements IntroductionInterceptor,IOther{
public void doOther(){
System.out.println("Other对象的功能");
}

public Object invoke(MethodInvocation methodInvocation) throws Throwable {
if(implementsInterface(methodInvocation.getMethod().getDeclaringClass())){
return methodInvocation.getMethod().invoke(this, methodInvocation.getArguments());
}
else{
return methodInvocation.proceed();
}
}
public boolean implementsInterface(Class arg0){
boolean assignableFrom = arg0.isAssignableFrom(IOther.class);
return assignableFrom;
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
<bean id="some" class="com.yourcompany.spring.Some" />
<bean id="other" class="com.yourcompany.spring.Other" />
<bean id="otherAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="other"></constructor-arg>
<constructor-arg value="com.yourcompany.spring.IOther" />
</bean>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.yourcompany.spring.ISome" />
<property name="target" ref="some" />
<property name="interceptorNames">
<list>
<value>otherAdvisor</value>
</list>
</property>
</bean>
</beans>

程序主入口:

package com.yourcompany.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Introduction{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ISome some=(ISome)context.getBean("proxyFactoryBean");
some.doSome();
((IOther)some).doOther();
}
}


另:Spring框架中的类DelegatingIntroductionInterceptor是接口DelegatingIntroductionInterceptor的默认实现类,用法大致相同。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息