一、
1.Introduction的作用是给类动态的增加方法
When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called belongs to the proxied bean or to the introduced interface.
二、例子
要为演出者增加encore功能
1.
package concert; public interface Performance {
void performance();
}
2.
package concert; import org.springframework.stereotype.Component; @Component
public class Eminem implements Performance{ @Override
public void performance() {
System.out.println("you can do anything set to your mind");
} }
3.
package concert; public interface Encoreable {
void performEncore();
}
4.
package concert; public class DefaultEncoreable implements Encoreable{ @Override
public void performEncore() {
System.out.println("DefaultEncoreable---> performEncore()");
} }
5.
package concert; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component; @Aspect
@Component
public class EncoreableIntroducer {
@DeclareParents(value = "concert.Performance+", defaultImpl = DefaultEncoreable.class)
public static Encoreable encoreable;
}
解析:
@DeclareParents的作用是把"Encoreable"介绍给"Performance",它由三部分组成
(1)value:要增加新功能的bean的接口类型
(2)defaultImpl:要增加的功能接口的实现
(3)增加的功能接口的静态变量
6.配置文件
package concert; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class Config { }
7.测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import concert.Config;
import concert.Encoreable; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Config.class)
public class IntroductionTest {
@Autowired
ApplicationContext context; @Test
public void eddieShouldBeAContestant() {
Encoreable eminem = (Encoreable) context.getBean("eminem");
eminem.performEncore();
// Performance eminem = (Performance) context.getBean("eminem");
// eminem.performance();
}
}
另外:xml的配置方法:
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+" implement-interface="concert.Encoreable" default-impl="concert.DefaultEncoreable" />
</aop:aspect>
As its name implies, <aop:declare-parents> declares that the beans it advises will have new parents in its object hierarchy. Specifically, in this case you’re saying that the beans whose type matches the Performance interface (per the types-matching attribute) should have Encoreable in their parentage (per the implement-interface attribute). The final matter to settle is where the implementation of the Encoreable ’s methods will come from.
还有另一个指定方法用delegate-ref属性
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+" implement-interface="concert.Encoreable" delegate-ref="encoreableDelegate" />
</aop:aspect>
The delegate-ref attribute refers to a Spring bean as the introduction delegate.This assumes that a bean with an ID of encoreableDelegate exists in the Spring context:
<bean id="encoreableDelegate" class="concert.DefaultEncoreable" />
The difference between directly identifying the delegate using default-impl and indirectly using delegate-ref is that the latter will be a Spring bean that itself may be injected, advised, or otherwise configured through Spring.
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <!--<start id="audience_bean"/>-->
<bean id="audience"
class="com.springinaction.springidol.Audience" />
<!--<end id="audience_bean"/>--> <!--<start id="audience_aspect"/>-->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression=
"execution(* com.springinaction.springidol.Performer.perform(..))"
/>
<aop:before
pointcut-ref="performance"
method="takeSeats()" />
<aop:before
pointcut-ref="performance"
method="turnOffCellPhones" />
<aop:after-returning
pointcut-ref="performance"
method="applaud" />
<aop:after-throwing
pointcut-ref="performance"
method="demandRefund" />
</aop:aspect> <!--<start id="contestant_introduction"/>-->
<aop:aspect>
<aop:declare-parents
types-matching="com.springinaction.springidol.Performer+"
implement-interface="com.springinaction.springidol.Contestant"
default-impl="com.springinaction.springidol.GraciousContestant"
/>
</aop:aspect>
<!--<end id="contestant_introduction"/>--> <!--
<start id="delegate_ref"/>
<aop:declare-parents
types-matching="com.springinaction.springidol.Performer+"
implement-interface="com.springinaction.springidol.Contestant"
delegate-ref="contestantDelegate"
/>
<end id="delegate_ref"/> <start id="contestant_delegate"/>
<bean id="contestantDelegate"
class="com.springinaction.springidol.GraciousContestant" />
<end id="contestant_delegate"/> --> </aop:config>
<!--<end id="audience_aspect" />--> </beans>