一、在不修改目标对象的源代码的情况下,为目标对象增加方法和属性。
二、实现方法
1、我们要将新增加的方法声明放在新的接口中。
public interface IOtherBean {
public void doOther();
}
2、编写引入类,实现拦截器IntroductionInterception和新方法接口。
public class SomeBeanIntroductionInterceptor implements IOtherBean, IntroductionInterceptor {
public void doOther() {
System.out.println("doOther!");
}
public Object invoke(MethodInvocation invocation) throws Throwable {
//判断调用的方法是否为指定类中的方法
if ( implementsInterface(invocation.getMethod().getDeclaringClass()) ) {
return invocation.getMethod().invoke(this, invocation.getArguments());
}
return invocation.proceed();
}
/**
* 判断clazz是否为给定接口IOtherBean的实现
*/
public boolean implementsInterface(Class clazz) {
return clazz.isAssignableFrom(IOtherBean.class);
}
}
3、配置引入类。
a、将拦截器实现和新方法接口名注入到DefaultIntroductionAdvisor中。
b、只能用构造器注入。
<!-- 通知者,只能以构造器方法注入-->
<bean id="introductionAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="someBeanAdvice" />
<constructor-arg value="aop.spring.introduction.IOtherBean" />
</bean>
<!-- 目标对象 -->
<bean id="someBeanTarget" class="aop.spring.introduction.SomeBeanImpl" />
<!-- 通知 -->
<bean id="someBeanAdvice" class="aop.spring.introduction.SomeBeanIntroductionInterceptor" />
<!-- 通知者,只能以构造器方法注入-->
<bean id="introductionAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="someBeanAdvice" />
<constructor-arg value="aop.spring.introduction.IOtherBean" />
</bean>
<!-- 代理 (将我们的切面织入到目标对象)-->
<bean id="someBeanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 若目标对象实现了代理接口,则可以提供代理接口的配置 -->
<property name="proxyInterfaces" value="aop.spring.introduction.ISomeBean" />
<!-- 配置目标对象 -->
<property name="target" ref="someBeanTarget" />
<!-- 配置切面 -->
<property name="interceptorNames">
<list>
<value>introductionAdvisor</value>
</list>
</property>
</bean>
详细配置信息
三、原理
1、用户调用方法时,拦截器IntroductionInterception将方法拦截,判断所调用方法是否为新方法接口里的方法。如果为true,则会让其去调用该方法是实现,否则让其去继续调用原先的方法。
2、在性能上有缺陷所以应该尽量少用。