标签:
假设第三方(or别的team)提供一个工厂类(此类是不能够改动的。往往以jar包形式提供的),须要供给我们项目来使用。
可是我们自己的项目使用了spring来配置,所以我们当然希望可以通过spring的aop来配置这个工厂类来,来创建实例以进行引用。
可是这个工厂类的源码比較特殊。例如以下:
package x.y; import java.util.Properties; import x.y.client.CmdClient; import x.y.client.InternalCmdClient; public class ClientFactory { private static volatile CmdClient cmdclient; private static volatile InternalCmdClient cmdinternalclient; private static Properties props = new Properties(); public static void setEnv(String env){ props.put( "env", env ); } public static CmdClient getCmdClient() throws Exception{ if ( cmdclient == null ) { synchronized ( CmdClientImpl.class ) { if ( cmdclient == null ) { cmdclient = new CmdClientImpl( props ); } } } return cmdclient; } public static InternalCmdClient getIntenalCmdClient() throws Exception{ if ( cmdinternalclient == null ) { synchronized ( InternalCmdClientImpl.class ) { if ( cmdinternalclient == null ) { cmdinternalclient = new InternalCmdClientImpl( props ); } } } return cmdinternalclient; } public static void destoryClient(){ if ( cmdclient != null ) { cmdclient.destroy(); cmdclient = null; } if ( cmdinternalclient != null ) { cmdinternalclient.destroy(); cmdinternalclient = null; } } }
介绍如上工厂类的特殊性:
可是此方法不可取,例如以下所述:
<bean id="internalClient" class="x.y.ClientFactory" factory-method="getIntenalCmdClient" destroy-method="destroy"> <property name="env" value="${env}" /> </bean>
所以此<property>的配置是无效的。
二:会有人说既然“静态工厂化方法”不可取,那么spring官方文档里面还提供了“实例工厂方法”。可是此方法也是不可取的,例如以下所述:
<bean id="clientFactory" class="x.y.ClientFactory"> <property name="env" value="${env}" /> </bean> <bean id="internalClient" factory-bean="clientFactory" factory-method="getIntenalCmdClient" destroy-method="destroy"> </bean>
<bean id="internalClient" class="x.y.AhClientFactory" factory-method="getIntenalCmdClient" destroy-method="destroy" depends-on="clientFactory"> </bean> <bean id="clientFactory" class="x.y.ClientFactory"> <property name="env" value="${env}" /> </bean>
BTW:
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5153658.html