在上一篇 Spring AOP实例 – advice, pointcut 和 advisor, 必须手动创建一个代理bean(ProxyFactryBean),对每个Bean需要AOP支持。
这不是一种有效的方式,例如,如果想在客户模块,所有的DAO类实现SQL日志支持(提醒)的AOP功能,那么必须手动创建很多代理工厂bean,因此在 bean配置文件可能会泛滥代理类。
幸运的是,Spring有两个自动代理创建者来自动创建代理bean。
在此之前,必须手动创建一个代理bean(ProxyFactryBean)。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.zyiz.customer.services.CustomerService"> <property name="name" value="zyiz Mook Kim" /> <property name="url" value="http://www.zyiz.net" /> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.zyiz.aop.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodzyizcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean> </beans>
使用代理名称“customerServiceProxy”来获得 bean。
CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");
在自动代理机制,只需要创建一个的 BeanNameAutoProxyCreator,并包含所有你的bean(通过bean的名字,或正则表达式名)和“advisor” 作为一个单位。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.zyiz.customer.services.CustomerService"> <property name="name" value="zyiz Mook Kim" /> <property name="url" value="http://www.zyiz.net" /> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.zyiz.aop.HijackAroundMethod" /> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodzyizcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> </beans>
现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。
CustomerService cust = (CustomerService)appContext.getBean("customerService");
这个 DefaultAdvisorAutoProxyCreator
是非常强大的,如果有 bean 相关连,Spring会自动创建一个代理。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.zyiz.customer.services.CustomerService"> <property name="name" value="zyiz Mook Kim" /> <property name="url" value="http://www.zyiz.net" /> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.zyiz.aop.HijackAroundMethod" /> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodzyizcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans>
不用管使用什么代理方法, Spring 都会有最适合处理方式。
下载代码 – http://pan.baidu.com/s/1pKdqtjt