Spring自動プロキシ作成者の例
最後のSpring AOPの例では、
advice
、
pointcut and advisor
AOPが必要なBeanごとにプロキシBean(ProxyFactoryBean)を作成します。
これは効率的な方法ではありません。たとえば、顧客モジュールのすべてのDAOクラスでSQLロギングのサポート(アドバイス)を使用してAOP機能を実装する場合、多くのプロキシファクトリBeanを手動で作成する必要があります。大量のプロキシBeanを使用してBean構成ファイルをフラッディングします。
幸運なことに、Springには2つの自動プロキシ作成機能があり、自動的にBeanのプロキシを作成します。
1. BeanNameAutoProxyCreatorの例
その前に、プロキシBean(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.mkyong.customer.services.CustomerService"> <property name="name" value="Yong Mook Kim"/> <property name="url" value="/"/> </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> </beans>
また、プロキシ名が “customerServiceProxy”のBeanでBeanを取得します。
CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");
自動プロキシメカニズムでは、
BeanNameAutoProxyCreator
を作成し、すべてのBean(Bean名、正規表現名を使用)と ‘Advisor’を1つのユニットに組み込むだけです。
<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.mkyong.customer.services.CustomerService"> <property name="name" value="Yong Mook Kim"/> <property name="url" value="/"/> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.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.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");
2. DefaultAdvisorAutoProxyCreatorの例
この
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.mkyong.customer.services.CustomerService"> <property name="name" value="Yong Mook Kim"/> <property name="url" value="/"/> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.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>
これはちょうど権力を超えています。なぜなら、どのBeanがプロキシであるべきかを制御することができないからです。あなたができることは、Springがあなたにとって最良のものであるという信頼です。
これをプロジェクトに実装したい場合は十分注意してください。