对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

使用“自动代理”功能

到目前为止,我们已经考虑了通过使用ProxyFactoryBean或类似的工厂bean显式创建AOP代理。spring-doc.cadn.net.cn

Spring 还允许我们使用“自动代理”bean定义,这可以自动代理选定的bean定义。这是基于 Spring 的“bean 后处理器”基础设施,该基础设施使容器加载时能够修改任何 bean 定义。spring-doc.cadn.net.cn

在此模型中,您在XML bean定义文件中设置了一些特殊的bean定义来配置自动代理基础设施。这使您可以声明符合条件的自动代理目标。您不必使用ProxyFactoryBeanspring-doc.cadn.net.cn

有两种方法可以做到这一点:spring-doc.cadn.net.cn

  • 通过使用自动代理创建器,该创建器引用当前上下文中的特定bean。spring-doc.cadn.net.cn

  • 自动代理创建的一个特殊情况值得单独考虑: 由源级元数据属性驱动的自动代理创建。spring-doc.cadn.net.cn

自动代理的Bean定义

本节涵盖了由org.springframework.aop.framework.autoproxy包提供的自动代理创建器。spring-doc.cadn.net.cn

BeanNameAutoProxyCreator

The BeanNameAutoProxyCreator 类是一个 BeanPostProcessor,它会自动为名称匹配字面值或通配符的 bean 创建 AOP 代理。以下示例展示了如何创建一个 BeanNameAutoProxyCreator bean:spring-doc.cadn.net.cn

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<property name="beanNames" value="jdk*,onlyJdk"/>
	<property name="interceptorNames">
		<list>
			<value>myInterceptor</value>
		</list>
	</property>
</bean>

ProxyFactoryBean一样,有一个interceptorNames属性而不是拦截器列表,以允许原型顾问的正确行为。名为“interceptors”的可以是顾问或任何建议类型。spring-doc.cadn.net.cn

与自动代理化一样,使用BeanNameAutoProxyCreator的主要目的是将相同的配置一致地应用于多个对象,并且配置量最小。它是将声明式事务应用于多个对象的流行选择。spring-doc.cadn.net.cn

Bean definitions whose names match, such as jdkMyBean and onlyJdk in the preceding example, are plain old bean definitions with the target class. An AOP proxy is automatically created by the BeanNameAutoProxyCreator. The same advice is applied to all matching beans. Note that, if advisors are used (rather than the interceptor in the preceding example), the pointcuts may apply differently to different beans。spring-doc.cadn.net.cn

DefaultAdvisorAutoProxyCreator

一个更通用且极其强大的自动代理创建器是 DefaultAdvisorAutoProxyCreator。这会自动在当前上下文中应用符合条件的顾问,而无需在自动代理顾问的bean定义中包含特定的bean名称。它提供了与BeanNameAutoProxyCreator相同的优点,即一致的配置和避免重复。spring-doc.cadn.net.cn

使用这种机制涉及:spring-doc.cadn.net.cn

  • 指定一个DefaultAdvisorAutoProxyCreator bean定义。spring-doc.cadn.net.cn

  • 在同一或相关上下文中指定任意数量的通知器。请注意,这些 必须是通知器,而非拦截器或其他增强。这是必要的, 因为必须存在一个切入点表达式来进行评估,以检查每个增强 对候选Bean定义的适用性。spring-doc.cadn.net.cn

The DefaultAdvisorAutoProxyCreator 自动评估每个顾问中包含的切入点,以确定它应该将什么(如果有)建议应用到每个业务对象(例如示例中的 businessObject1businessObject2)。spring-doc.cadn.net.cn

这意味着可以自动将任意数量的顾问应用到每个业务对象上。如果任何顾问中的切点都不匹配业务对象中的任何方法,则该对象不会被代理。随着为新的业务对象添加 bean 定义,如果需要,它们会自动被代理。spring-doc.cadn.net.cn

自动代理在一般情况下具有一个优势,即使得调用者或依赖项无法获得未被代理的对象。在这个ApplicationContext上调用getBean("businessObject1")返回的是一个AOP代理,而不是目标业务对象。(前面展示的“内部bean”惯用法也提供了这个好处。)spring-doc.cadn.net.cn

以下示例创建一个DefaultAdvisorAutoProxyCreator bean 和本节中讨论的其他元素:spring-doc.cadn.net.cn

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
	<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>

<bean id="customAdvisor" class="com.mycompany.MyAdvisor"/>

<bean id="businessObject1" class="com.mycompany.BusinessObject1">
	<!-- Properties omitted -->
</bean>

<bean id="businessObject2" class="com.mycompany.BusinessObject2"/>

The DefaultAdvisorAutoProxyCreator 非常有用,如果你想一致地将相同的建议应用到许多业务对象上。一旦基础设施定义就位,你可以在不包含特定代理配置的情况下添加新的业务对象。你还可以轻松地引入额外的切面(例如,跟踪或性能监控切面),而对配置的更改最小。spring-doc.cadn.net.cn

The DefaultAdvisorAutoProxyCreator 提供了过滤支持(通过使用命名约定,使得只有特定的顾问被评估,这允许在同一个工厂中使用多个配置不同的 AdvisorAutoProxyCreators)。顾问可以实现 org.springframework.core.Ordered 接口以确保正确的排序。如果这是一个问题,TransactionAttributeSourceAdvisor 在前面的例子中有可配置的顺序值。默认设置是无序的。spring-doc.cadn.net.cn