平台:Spring2.5
Introduction是个特别的Advice,它“影响了目标对象的行为定义,直接增加了目标对象的职责”。可通过org.springframework.aop.IntroductoinInterceptor来实现Introduction。
IntroductionInterceptor继承了MethodInterceptor与DynamicIntroductionAdvice接口,其中implementsInterfaces()方法(继承自DynamicIntroductionAdvice)如果传回true,表示目前的IntroductionInterceptor实现了规定的接口(也就是要额外增加行为的接口),此时您要使用invoke()执行接口上的方法,让目标对象执行额外的行为,您不可使用MethodInvocation的proceed()方法,因为要执行的是对象上原来没有的行为,执行proceed()方法没有意义。
org.springframework.aop.support.DelegatingIntroductionInterceptor是SpringAOP中为IntroductionInterceptor接口所提供的实现类。
ILockable.java内容:
package onlyfun.caterpillar;
public interface ILockable {
public void lock();
public void unlock();
public boolean isLocked();
}
LockIntroduction.java内容:
package onlyfun.caterpillar;
import org.springframework.aop.
support.DelegatingIntroductionInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.
framework.AopConfigException;
public class LockIntroduction
extends DelegatingIntroductionInterceptor
implements ILockable {
private boolean locked;
public Object invoke(MethodInvocation invocation)
throws Throwable {
// locked为true下不能调用set方法
if (isLocked() &&
invocation.getMethod().
getName().indexOf("set") == 0) {
throw new AopConfigException(
"对象被锁定!!");
}
return super.invoke(invocation);
}
public void lock() {
locked = true;
}
public void unlock() {
locked = false;
}
public boolean isLocked() {
return locked;
}
}
ISome.java内容:
package onlyfun.caterpillar;
public interface ISome {
public void setSome(String some);
public String getSome();
}
Some.java内容:
package onlyfun.caterpillar;
public class Some implements ISome {
private String some;
public void setSome(String some) {
this.some = some;
}
public String getSome() {
return some;
}
}
application.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="lockIntroduction"
class="onlyfun.caterpillar.LockIntroduction"/>
<bean id="lockAdvisor"
class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="lockIntroduction"/>
<constructor-arg value="onlyfun.caterpillar.ILockable"/>
</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>lockAdvisor</value>
</list>
</property>
</bean>
</beans>
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
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) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"beans-config.xml");
ISome some =
(ISome) context.getBean("proxyFactoryBean");
//对象没有被锁定,可以调用set方法
some.setSome("justin");
System.out.println(some.getSome());
try {
//对象被锁定
((ILockable) some).lock();
//无法调用set方法,抛出异常
some.setSome("momor");
//由于会抛出异常,所以下面的这行程序无法被执行
System.out.println(some.getSome());
}
catch(Throwable e) {
e.printStackTrace();
}
// Object is unlocked.
((ILockable) some).unlock();
// It's ok to use setter again.
some.setSome("momor");
System.out.println(some.getSome());
}
}