Spring 切面 AOP基础 之三

来源:互联网 发布:知乎公司地址邮编 编辑:程序博客网 时间:2024/05/30 13:41

螃蟹在剥我的壳,脚往杯上落,茶叶煮着温泉,而代码在撸我。看见一手动人的小诗,仿写了一手,上回说到锋利的切面AOP,这里对他进行自动代理,灰常的powerfull。

1、通常手写对Service的代理十分繁琐,可以用BeanNameAutoProxyCreator去根据名字自动代理,还有个DefaultAdvisorAutoProxyCreator更具Advicer匹配成功自动创建代理。

Spring-Customer.xml 

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   <bean id="customerService" class="com.mkyong.customer.services.CustomerService" >   <property name="name" value="Yong Mook Kim" />   <property name="url" value="http://www.mkyong.com" />   </bean>    <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.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.NameMatchMethodPointcutAdvisor"><property name="mappedName" value="printName" /><property name="advice" ref="hijackAroundMethodBeanAdvice" /></bean><!--<bean id="customerAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="patterns"><list><value>.*URL.*</value></list></property><property name="advice" ref="hijackAroundMethodBeanAdvice" /></bean>--><!-- auto proxy creator --><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><!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> --></beans>





0 0