Spring 3シリーズ11-Spring AOP-自動作成Proxy
16995 ワード
Spring 3シリーズ11-Spring AOP-自動作成Proxy
「Spring 3シリーズ9-Spring AOP——Advice」および「Spring 3シリーズ10-Spring AOP-Pointcut、Advisorブロック指定方法」の例では、各AOPが必要なbeanのために手動でProxy beanを作成しなければならない.
これはいい経験ではありません.例えば、DAO層のすべてのbeanにAOPをサポートしてもらいたいです.SQLログを書くためには、多くのProxyFactoryBenを手作りで作成しなければなりません.そうすると、直接にあなたのxml配置ファイルの内容が幾何級数的に倍増され、xml配置の維持に役立ちません.
幸い、Springには二つの方法があります.あなたのために自動的にproxyを作成することができます.
1. BenName AutoProxyCreatorを利用して自動的にproxyを作成します.
手作りProxyFactoryBeanは以下の通りです.
配置が完了したらcustomerServiceProxyを得るには、次のコードが必要です.
CustoomerService cust=(CustoomerService)ap Contact.get Ben("customer ServiceProxy")
自動モードでは、BeanName AutoProxyCreatorを作成し、すべてのbean(名前または正規表現で一致する)とadvisorを独立したユニットに構成する必要があります.
以上の配置では、beanのidが*Serviceに該当すると自動的にproxyが作成されますので、以下のコードでproxyが得られます.
Custoomer Service cust=(CustoomerService)ap Contact.get Bean("customer Service")
2. Default AdvisoorAutoxyCreatorを利用してProxyを作成します.
この方式はDefault Advisor AutoProxyCreatorを利用して自動的にProxyを作成することができます.この方式は威力が高く、Advisorのbeanにマッチすると、自動的にProxyを作成してAOPを実現しますので、慎重に使います.
上記の例では、xmlのいずれかのbeanは、methodの名前がprintNameである限り、以下のコードを使用すると、自動的にProxyを作成し、AOPをサポートします.
Custoomer Service cust=(CustoomerService)ap Contact.get Bean("customer Service")
「Spring 3シリーズ9-Spring AOP——Advice」および「Spring 3シリーズ10-Spring AOP-Pointcut、Advisorブロック指定方法」の例では、各AOPが必要なbeanのために手動でProxy beanを作成しなければならない.
これはいい経験ではありません.例えば、DAO層のすべてのbeanにAOPをサポートしてもらいたいです.SQLログを書くためには、多くのProxyFactoryBenを手作りで作成しなければなりません.そうすると、直接にあなたのxml配置ファイルの内容が幾何級数的に倍増され、xml配置の維持に役立ちません.
幸い、Springには二つの方法があります.あなたのために自動的にproxyを作成することができます.
1. BenName AutoProxyCreatorを利用して自動的にproxyを作成します.
手作りProxyFactoryBeanは以下の通りです.
<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean>
<bean id="hijackAroundMethodBean" class=" com.lei.demo.aop.advice.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=" hijackAroundMethodBean " />
</bean>
</beans>
配置が完了したらcustomerServiceProxyを得るには、次のコードが必要です.
CustoomerService cust=(CustoomerService)ap Contact.get Ben("customer ServiceProxy")
自動モードでは、BeanName AutoProxyCreatorを作成し、すべての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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class=" com.lei.demo.aop.advice.HijackAroundMethod" />
<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 id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
</beans>
以上の配置では、beanのidが*Serviceに該当すると自動的にproxyが作成されますので、以下のコードでproxyが得られます.
Custoomer Service cust=(CustoomerService)ap Contact.get Bean("customer Service")
2. Default AdvisoorAutoxyCreatorを利用してProxyを作成します.
この方式はDefault Advisor AutoProxyCreatorを利用して自動的にProxyを作成することができます.この方式は威力が高く、Advisorのbeanにマッチすると、自動的にProxyを作成してAOPを実現しますので、慎重に使います.
<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.lei.demo.aop.advice.HijackAroundMethod" />
<bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>
上記の例では、xmlのいずれかのbeanは、methodの名前がprintNameである限り、以下のコードを使用すると、自動的にProxyを作成し、AOPをサポートします.
Custoomer Service cust=(CustoomerService)ap Contact.get Bean("customer Service")