Oracle神谕

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  284 随笔 :: 9 文章 :: 106 评论 :: 0 Trackbacks

Using the ProxyFactoryBean to create AOP proxies
If you're using the Spring Ioc container (an applicationContext or BeanFactory) for you business object--and you should be!-- you will want to use one of Spring's AOP fatoryBeans.(Remeber that a factory bean introduces a layer of indirection, enabling it to create objects of different type).

The basic way to create an AOP proxy in Spring to use the org.springframework.aop.framework.ProxyFactoryBean. This gives complete control over the pointcuts an advice that will apply, and theire ordering . However ,there are simpler options that are preferable(更可取的、更好的) if you don't need such control.

Basics
The proxyFactoryBean,like other Spring FactoryBean implementations,introduces a level of indirection(间接). If you define a ProxyFactoryBean with name foo,what objects referencing foo see is not the ProxyFactoryBean instance itself, but an object created by the ProxyFactoryBeans's implementation of the getObject() method. This method will create an AOP proxy wrapping a target object.

One of the most important benefits of using a ProxyFactoryBean or other IoC-aware to create AOP proxies, it that it means that advices and pointcuts can also be managed by IoC. This is a powerful feature , enabling certain approaches that are hard to achieve with other AOP frameworks. For example,an advice may itself reference application objects(besides the target , which should be available in any AOP framework),benefiting from all the pluggability provided by Dependency Injection.

JavaBean properties
Like most FactoryBean implementations provided with Spring, ProxyfactoryBean is itself a JavaBean. It properties are used to:
Specify the target you  want to proxy
Specify whether to use CGLIB

Some key properties are inherited from org.springframework.aop.framework.ProxyConfig: the subclass for all AOP proxy factories. These include:
proxyTargetClass: true if we should proxy the target class,rather than its interfaces. If this  is true we need to use CGLIB.

optimize: whether to apply aggressive optimization to created proxies. Don't use this setting unless you  understand how the relevant(相关的) AOP proxy handles optimization. This is currently used only for CGLIB proxies;it has no effect with  JDK dynamic proxies(the default).

frozen:whether avice changes should be disallowed once the proxy factory has bean configured. Default is false.

exposeProxy: whether the current proxy should be exposed in a ThreadLocal so that it can be accessed by the target (It's available via the MethodInvocation without the need for a ThreadLocal) If a target needs to obtain the proxy and exposedProxy is true, the target can use the AopContext.currentProxy() method.

aopProxyFactory: the implementation of AopProxyFactory to use. Offers a way of customizing whether to use dynammic proxies,CGLIB or any other proxy strategy. The default implementation will choose dynamic proxies or CGLIB appropriately. There should be need to use this property, it's intended to allow the addition of new proxy types in spring 1.1.

Other properties specific to ProxyFactoryBean include:
.proxyInterfaces: array of String interface names.  If this isn't supplied, a CGLIB proxy for the target class will be used.
.interceptorNames:String array of Advisor,interceptor or other advice names to apply.Ordering is sugnicicant. first come,first serve that is. The first interceptor in the list will be the first to be able to interceptor the invocation (of course if it concerns a regular MethodInterceptor or BeforeAdvice. The names are bean names in the current factory , including  bean names from ancestor factories. You  can't mention bean references here since doing so would result iin the ProxyFactoryBean ignoring the singleton  setting of the advise. you can append an iinterceptor name with an asterisk(*).  This will result  in the application of all advisor beans withe names starting with the part before the asterisk to be applied.  An example of using this feature can be found below.

Singleton: whether or not the factory should return a single object, no matter how often the getObject() method is called. Server FactoryBean implementations offer such a method. Default value is true. If you want to use stateful advice --for example ,for stateful mixins-user prototype advices along withe s singleton value of false.

Proxying interfaces

<bean id="personTarget" class="com.mycompany.PersionImpl">
   <property name="name"><value>Tony</value></property>
   <property name="age"><value>51</value></property>  
</bean>

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
   <property name="someProperty"><value>Custom string property value</value></property>
</bean>

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInteceptor" ></bean>

<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
   <property name="proxyInterface"><value>com.company.Person</value></property>
  
   <property name="target"><ref local="personTarget"/></property>
   <property name="interceptorNames">
      <list>
         <value>myAdvisor</value>
         <value>debugInterceptor</value>
      </list>
   </property>
</bean>

 

posted on 2005-07-07 10:43 java世界畅谈 阅读(454) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: