SpringAOP中的IntroductionInterceptor

55 篇文章 0 订阅
33 篇文章 0 订阅

        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的默认实现类,用法大致相同。


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值