Spring Gossip: IntroductionInterceptor

本文介绍如何通过Spring AOP的Introduction机制为现有类动态添加新的接口和方法,无需修改原有类的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于之前介绍过的Before Advice、After Advice、Around Advice、Throw Advice,从使用者的角度来看,它们“影响了目标物件上某些方法的行为”,例如让某些方法看来似乎增加了一些记录的动作。

Introduction是个特别的Advice,从使用者的角度来看,它“影响了目标物件的行为定义,直接增加了目标物件的职责(具体来说就是增加了可操作的方法)”,例如让某个已定义好的物件,在不修改该物件之类别档案的情况下,却可以增加一些额外的操作方法到物件之上。

就Java程式语言类别设计的观点来说,动态为物件增加可操作的方法显得不可思议,事实上在Spring AOP中,您可以透过实作org.springframework.aop.IntroductionInterceptor来实现Introduction。

IntroductionInterceptor继承了MethodInterceptor与DynamicIntroductionAdvice介面,其中implementsInterface()方法(继承自DynamicIntroductionAdvice)如果返回true的话,表示目前的 IntroductionInterceptor实作了给定的介面(也就是要额外增加行为的介面),此时您要使用invoke()呼叫介面上的方法,让目标物件执行额外的行为,您不可能使用MethodInvocation的proceed()方法,因为您要执行的是物件上原来没有的行为,呼叫 proceed()方法没有意义。

从文字上来理解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("原来物件的职责。。。");
}
}


您希望在不修改原始档案的情况下,为Some类别增加一些可操作的方法,也许您甚至连原始码档案都没有,只有.class档案,您唯一知道的也许是他们的API说明,在不对它们作出修改的情况下,您希望Some类别可以增加doOther()方法。

在Spring中,您可以藉由实作IntroductionInterceptor介面来完成上面的任务,首先您为doOther()方法建立介面:

* IOther.java

package onlyfun.caterpillar;

public interface IOther {
public void doOther();
}


接着定义一个OtherIntroduction类别实作IntroductionInterceptor介面,并在实作IntroductionInterceptor介面的同时,也实作IOther介面,例如:

* 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("增加的职责。。。");
}
}


接着您要在Bean定义档中将Introduction缝合至Some物件之上,使用org.springframework.aop.support.DefaultIntroductionAdvisor就可以了,例如:

* beans-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<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 index="0">
<ref bean="otherIntroduction"/>
</constructor-arg>
<constructor-arg index="1">
<value>onlyfun.caterpillar.IOther</value>
</constructor-arg>
</bean>

<bean id="proxyFactoryBean"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>onlyfun.caterpillar.ISome</value>
</property>
<property name="target">
<ref bean="some"/>
</property>
<property name="interceptorNames"> DefaultIntroductionAdvisor的bean id
<list>
<value>otherAdvisor</value>
</list>
</property>
</bean>
</beans>

DefaultIntroductionAdvisor在建构时,需要给它IntroductionInterceptor的实例,以及所要代理额外行为的介面,现在,来撰写一个简单的程式测试,从这个程式当中,您可以更进一步了解何谓为物件额外增加行为:

* SpringAOPDemo.java

package onlyfun.caterpillar;

import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.FileSystemXmlApplicationContext;

public class SpringAOPDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");

ISome some = (ISome) context.getBean("proxyFactoryBean");

some.doSome();

// 看来好像some物件动态增加了职责
((IOther) some).doOther();
}
}

对于some所参考的物件来说,它原先并不会有doOther()方法可供操作,然而透过Spring AOP的Introduction机制,现在some所参考的物件多了doOther()方法可以操作。(introduction解释为传入)
<br>认识 Spring <br> 来认识 Spring 的一些特性,并初步了解一下什么叫作 IoC?什么叫作 DI? <br>简介 Spring <br>Inversion of Control <br>Dependency Injection <br><br><br>核心容器 <br> Spring 核心容器实作了 IoC,BeanFactory 与 ApplicationContext 的运用是了解 Spring 的重点所在。 <br>管理 Bean <br>从第一个 Spring 应用程式开始,逐步了解何谓依赖注入,以及如何使用 Spring 的容器功能来管理 Bean,了解 BeanSpring 容器中的生命周期。<br>第一个 Spring 程式 <br>BeanFactory、 ApplicationContext <br>Type 2 IoC、Type 3 IoC <br>属性参考 <br>自动绑定 <br>集合物件注入 <br>Bean 的生命周期 <br>Bean 进阶管理 <br>理想上对于 Bean 来说,它不需要意识到 Spring 容器的存在,然而有时候 Bean 仍必须知道有关于 Spring 容器或自己的一些讯息,而另一方面,您可能必须让容器对 Bean 进行一些额外处理。<br>不使用XML定义档进行 Bean设置 <br>Aware 相关介面 <br>BeanPostProcessor <br>BeanFactoryPostProcessor <br>PropertyPlaceholderConfigurer <br>PropertyOverrideConfigurer <br>CustomEditorConfigurer <br>讯息与事件 <br>ApplicationContext 除了具备如 BeanFactory 基本的容器管理功能之外,并支援更多应用程式框架的特性,像是资源的取得、讯息解析、事件的处理与传播。<br>Resource 的取得 <br>解析文字讯息 <br>倾听事件 <br>事件传播 <br><br><br>AOP(Aspect-Oriented Programming) <br> 在一个服务的流程中插入与服务无关的逻辑(例如Logging、Security),这样的逻辑称为 Cross-cutting concerns,将 Crossing-cutting concerns 独立出来为一个物件,这样的特殊物件称之为 Aspect,Aspect-oriented programming 着重在 Aspect 的设计及与应用程式的缝合(Weave)。<br><br>AOP 入门 <br>AOP 的观念与术语都不是很直觉,可以先从代理机制(Spring 实现 AOP 的一种方式)来看看实际的例子,从而了解 AOP 的观念与各种术语。<br>从代理机制初探 AOP <br>动态代理<br><br>AOP 观念与术语 <br>Spring AOP <br>Advices <br>Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。<br>Before Advice <br>After Advice <br>Around Advice <br>Throw Advice <br>Pointcut、Advisor <br>Pointcut 定义了 Advice 的应用时机,在 Spring 中,使用 PointcutAdvisor 将 Pointcut 与 Advice 结合成为一个物件,Spring 中大部分内建的 Pointcut 都有对应的 PointcutAdvisor。<br>NameMatchMethodPointcutAdvisor <br>RegExpMethodPointcutAdvisor <br>ControlFlowPointcut <br>Pointcut 介面 <br>Pointcut 交集、联集操作 <br>Introduction <br>为特殊的 Advice,它影响的不是方法的流程,而是影响整个物件的行为,为物件动态 mixin 职责。<br>IntroductionInterceptor <br>DelegatingIntroductionInterceptor <br>Autoproxing <br>自动代理可以让您不用为每一个要被 Advised 的 Target 手动定义代理物件,透过 Bean 名称或是 Pointcut 的比对,自动为符合的 Target 建立代理物件。<br>BeanNameAutoProxyCreator <br>DefaultAdvisorAutoProxyCreator <br><br><br>持久层 <br> 来看看 Spring 的 IoC 容器与 AOP 框架如何应用于持久层,包括了资料库、交易等相关议题。 <br>资料库存取 <br>Spring 提供了 DAO 框架,让应用程式开发时无须耦合于特定资料库技术。<br>Spring 的 DAO 支持 <br>DataSource 注入 <br>DataSource 置换 <br>JDBC 支援 <br>Spring 在 JDBC 的使用上提供了几个类别,让您可以简化 JDBC 在使用时的流程。<br>使用 JdbcTemplate <br>JdbcTemplate 执行与更新<br><br>JdbcTemplate - 查询 <br>以物件方式进行操作 <br>DataFieldMaxValueIncrementer <br>交易管理 <br>Spring 提供编程式的交易管理(Programmatic transaction management)与宣告式的交易管理(Declarative transaction management),为不同的交易实作提供了一致的编程模型。<br>Spring 对交易的支援 <br>JDBC 编程式交易管理 <br>JDBC 宣告式交易管理 <br>交易的属性介绍 <br>TransactionAttributeSource、 TransactionAttribute <br>Hibernate 支援 <br>Spring 整合了对 Hibernate 的设定,并提供有 HibernateTemplate 等类别,让您在结合 Hibernate 时可以简化使用上的流程。<br>第一个 Hibernate 程式 <br>SessionFactory 注入 <br>HibernateTemplate <br>Hibernate 编程交易管理 <br>Hibernate 宣告式交易管理 <br><br><br>Web 层 <br> Spring 提供了 MVC Web 框架,您可以善用 IoC 容器在依赖注入上的好处,另一方面,Spring 也致力于与其它的 Web 框架的整合。 <br>Spring MVC 入门 <br>从一个最简单的 Spring Web 应用程式,来看看 Spring MVC 框架的架构与 API 组成元素。<br>第一个 Spring MVC 程式 <br>WebApplicationContext <br>Handler Mapping <br>Handler Interceptor <br>Controller 继承架构 <br>ModelAndView <br>View Resolver <br>Exception Resolver <br>使用 Controller 相关类别 <br>与其它 Web 框架的 Action 物件不同的是,Spring 提供了丰富的 Controller 相关类别,让您可以依需求来制作自己所需的 Controller 物件。<br>AbstractController <br>MultiActionController 与 ParameterMethodNameResolver <br>MultiActionController 与 PropertiesMethodNameResolver <br>ParameterizableViewController <br>AbstractCommandController <br>AbstractFormController <br>SimpleFormController <br>AbstractWizardFormController <br>ThrowawayController <br>搭配 Controller 的类别 <br>介绍如何在 Controller上搭配使用验证器(Validator)、如何实作Command资料的型态转换,以及如何使用Spring的相关API来实作档案上传的功能。<br>实作 Validator <br>使用 PropertyEditor <br>档案上传<br><br><br><br>View层方案、Web框架整合 <br> 当使用JSP作为View层技术时,您可以结合JSTL以及Spring提供的标签,而除了JSP技术作为View层之外,Spring还提供了不同 View层技术的解决方案,您甚至可以定义自己的View层技术实现。 <br>JSP View 层 <br>当使用 JSP 作为 View 层技术时,您可以结合 JSTL 以及 Spring 提供的标签。<br>结合 JSTL <br><spring:bind> 标签 <br>数据绑定的几个方法 <br><spring:message> 标签 <br><spring:transform> 标签 <br>其它 View 层 <br>除了 JSP View 层技术之外,您还可以使用其它的 View 层技术,或建立自己的 View Class。<br>以 Tiles 为例 <br>自订 View Class <br>与其它 Web 框架的整合 <br>您可以将 Spring 与现在的一些 Web 框架结合在一起,重点都在于如何让 Web 框架意识到 Spring 的存在。<br>第一个 Struts 程式 <br>在 Struts 中整合 Spring <br>第一个 JSF 程式 <br>在 JSF 中整合 Spring <br><br><br>其它 <br> Spring 提供了简化且一致的方式,让您在使用一些 API 或服务时更加简单。 <br>远程(Remoting) <br>Spring 提供了一致的使用方式,即使所采用的远程服务技术不尽相同,在 Spring 中运用它们的方式却是一致的。<br>RMI <br>Hessian、 Burlap <br>Http Invoker <br>邮件 <br>对于邮件发送服务的支援是由Spring的 org.springframework.mail.MailSender介面所定义,它有两个实作类别, org.springframework.mail.cos.CosMailSenderImpl与 org.springframework.mail.javamail.JavaMailSenderImpl。<br>简单邮件 <br>HTML 邮件 <br>内嵌图片或附档 <br>排程 <br>Spring则对 java.util.Timer提供了抽象封装,让您可以善用Spring的容器管理功能,而Spring对Quartz进行了封装,让它在使用上更加方便。<br>使用 TimerTask <br>使用 MethodInvokingTimerTaskFactoryBean <br>使用 Quartz <br>使用 MethodInvokingJobDetailFactoryBean <br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值