| 咖啡 的个人资料Separate Lives照片日志列表 | 帮助 |
|
2009/8/13 Using the ProxyFactoryBean to create AOP proxiesUsing the ProxyFactoryBean to create AOP proxies 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 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 Some key properties are inherited from org.springframework.aop.framework.ProxyConfig: the subclass for all AOP proxy factories. These include: 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: 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"> <bean id="myAdvisor" class="com.mycompany.MyAdvisor"> <bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInteceptor" ></bean> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean"> |
|
|