# 6.8使用auto-proxy功能

到目前为止，我们已经考虑通过使用ProxyFactoryBean或类似的工厂bean显式地创建AOP代理。

Spring还允许我们使用“auto-proxy”bean定义，它可以自动代理选定的bean定义。这是在Spring的“bean post processor”基础设施上构建的，它支持在容器加载时修改任何bean定义。

在这个模型中，你在XML bean定义文件中设置了一些特殊的bean定义，以配置自动代理基础结构。这允许你声明符合自动代理条件的目标。你不需要使用ProxyFactoryBean。

有两种方法可以做到这一点：

* 通过使用在当前上下文中引用特定bean的自动代理创建者。
* 一个需要单独考虑的自动代理创建的特殊情况：由源代码级元数据属性驱动的自动代理创建。

## 6.8.1 Auto-proxy Bean定义

本节介绍由org.springframework.aop.framework.autoproxy包提供的自动代理创建者。

**BeanNameAutoProxyCreator**

BeannameAutoProxyCreator类是一个BeanPostProcessor，它自动为名称与文字值或通配符匹配的Bean创建AOP代理。下面的示例演示如何创建BeanNameAutoProxyCreator bean：

```markup
<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属性而不是一个拦截器列表，以允许原型顾问的正确行为。命名的“拦截器”可以是顾问或任何建议类型。

与一般的自动代理一样，使用BeanNameAutoProxyCreator的主要目的是将相同的配置一致地应用于多个对象，配置量最小。它是将声明性事务应用于多个对象的常用选择。

名称匹配的bean定义（如前面示例中的jdkMyBean和onlyjdk）是带有目标类的普通旧bean定义。AOP代理由BeanNameAutoProxyCreator自动创建。同样的建议也适用于所有匹配的bean。注意，如果使用advisors（而不是前面示例中的拦截器），则切入点可能会不同地应用于不同的bean。

**DefaultAdvisorAutoProxyCreator**

一个更通用和强大的自动代理创建者是DefaultAdvisorAutoProxyCreator。这将在当前上下文中自动应用合格的advisors，而不需要在自动代理顾问的bean定义中包含特定的bean名称。它提供了与BeanNameAutoProxyCreator相同的配置和避免重复的优点。

使用此机制涉及：

* 指定DefaultAdvisorAutoProxyCreator bean定义。
* 在相同或相关的上下文中指定任意数量的顾问。请注意，这些必须是顾问，而不是拦截器或其他建议。这是必要的，因为必须有一个切入点来评估，以检查每个建议对候选bean定义的合格性。

DefaultAdvisorAutoProxyCreator自动评估每个顾问中包含的切入点，以查看它应该应用于每个业务对象（如示例中的BusinessObject1和BusinessObject2）的建议。

这意味着任何数量的顾问都可以自动应用于每个业务对象。如果任何顾问中没有与业务对象中的任何方法匹配的切入点，则不会代理该对象。当为新的业务对象添加bean定义时，如果需要，它们将被自动代理。

一般来说，自动代理的优点是使调用者或依赖项无法获得未建议的对象。在此applicationContext上调用getbean（“businessobject1”）将返回AOP代理，而不是目标业务对象。（前面显示的“内部bean”也提供了这个好处。）

下面的示例创建一个DefaultAdvisorAutoProxyCreator bean和本节讨论的其他元素：

```markup
<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"/>
```

如果你希望将相同的建议一致地应用于许多业务对象，那么DefaultAdvisorAutoProxyCreator非常有用。一旦基础结构定义就位，就可以添加新的业务对象，而不包括特定的代理配置。你还可以通过对配置的最小更改轻松地进入其他方面（例如，跟踪或性能监视方面）。

DefaultAdvisorAutoProxyCreator支持过滤（通过使用命名约定，只评估某些advisors ，从而允许在同一工厂中使用多个不同配置的AdvisorAutoProxyCreators）和排序。Advisors可以实现org.springframework.core.Ordered接口，以确保在出现问题时正确排序。在前面的示例中使用的TransactionAttributeSourceAdvisor具有可配置的order值。默认设置是无序的。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flydean.com/spring-framework-documentation5/core-technologies/6.spring-aop-apis/6.8auto-proxy-facility.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
