Introduction是个特别的Advice,它“影响了目标对象的行为定义,直接增加了目标对象的职责”。可通过org.springframework.aop.IntroductoinInterceptor来实现Introduction。 IntroductionInterceptor继承了MethodInterceptor与DynamicIntroductionAdvice接口,其中implementsInterfaces()方法(继承自DynamicIntroductionAdvice)如果传回true,表示目前的IntroductionInterceptor实现了规定的接口(也就是要额外增加行为的接口),此时您要使用invoke()执行接口上的方法,让目标对象执行额外的行为,您不可使用MethodInvocation的proceed()方法,因为要执行的是对象上原来没有的行为,执行proceed()方法没有意义。
Introduction是个特别的Advice,它“影响了目标对象的行为定义,直接增加了目标对象的职责”。可通过org.springframework.aop.IntroductoinInterceptor来实现Introduction。
ISome.java内容:
package onlyfun.caterpillar;
public interface ISome {
public void doSome();
}
Some.java内容:
package onlyfun.caterpillar;
public class Some implements ISome {
public void doSome() {
System.out.println("原来对象的职责。。。");
}
}
IOther.java内容:
package onlyfun.caterpillar;
public interface IOther {
public void doOther();
}
OtherIntroduction.java内容:
package onlyfun.caterpillar;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;
public class OtherIntroduction
implements IntroductionInterceptor, IOther {
//是否实现自IOther接口
public boolean implementsInterface(Class clazz) {
return clazz.isAssignableFrom(IOther.class);
}
public Object invoke(MethodInvocation methodInvocation)
throws Throwable {
//如果调用的方法来自IOther接口的定义
if(implementsInterface(
methodInvocation.getMethod().getDeclaringClass())) {
//调用额外加入(mixin)的行为
return methodInvocation.getMethod().
invoke(this, methodInvocation.getArguments());
}
else {
return methodInvocation.proceed();
}
}
public void doOther() {
System.out.println("增加的职责...");
}
}
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.0.xsd">
<bean id="some"
class="onlyfun.caterpillar.Some"/>
<bean id="otherIntroduction"
class="onlyfun.caterpillar.OtherIntroduction"/>
<bean id="otherAdvisor"
class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="otherIntroduction"/>
<constructor-arg value="onlyfun.caterpillar.IOther"/>
</bean>
<bean id="proxyFactoryBean"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="onlyfun.caterpillar.ISome"/>
<property name="target" ref="some"/>
<property name="interceptorNames">
<list>
<value>otherAdvisor</value>
</list>
</property>
</bean>
</beans>
test.java内容:
package onlyfun.caterpillar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class test{
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"beans-config.xml");
ISome some =
(ISome) context.getBean("proxyFactoryBean");
some.doSome();
//看来好像some对象动态增加了职责
((IOther) some).doOther();
}
}
log4j.properties
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n