请问spring如何动态实例化对象? 那你就在spring的配置文件里配置把你的属性需要什么类型,就引用相应的bean <ref bean="" /> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那个“type”参数怎么让spring知道? 一般的不是这样吗, <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="excelDao" class="com.gwssi.datacollect.dao.impl.ExcelDataCollectDAOImpl"> <property name="template" ref="jdbcTemplate" /> </bean>excelDao 引用一个事先配置好的bean-“jdbcTemplage”,这样是固定的就引用那个“jdbcTemplate”bean!问题是,我需要根据传入的不同的参数引用不同的bean呀! 与工厂模式相比Spring的ApplicationContext模式是更好的一种方式。在这里来说Spring相当于一个Bean容器,你将所有的Bean放入Spring中再根据bean id来获得。对于楼主这个例子,你有2种做法:第1种就是将你工厂中能够加工的bean分别配入Spring中。而id名就是你的type。这样你就可以根据通过的id来获得你不同的bean。第2种就是将你的工厂类配入Spring。在需要生产是,获取工厂类实例再获取生产出来的bean实例。lz仔细看下spring的reference的前几部分。你的理解有些偏差。 请问“pizzame”,您能否详细演示一下您说的那两种方法?小弟对spring实在是不熟,虚心向您请教! <beans> <bean id="dog" class="lee.BeingFactory" factory-method="getBeing"> <constructor-arg> <value>dog</value> </constructor-arg> <property name="msg"> <value>我是狗</value> </property> </bean> <bean id="cat" class="lee.BeingFactory" factory-method="getBeing"> <constructor-arg> <value>cat</value> </constructor-arg> <property name="msg"> <value>我是猫</value> </property> </bean>有两个例子,你自己看看,根据传入的参数不同,生成不同的实例,工厂bean是BeingFactory,工厂方法是getBeing()<beans> <bean id="personFactory" class="lee.PersonFactory"/> <bean id="chinese" factory-bean="personFactory" factory-method="getPerson"> <constructor-arg> <value>chin</value> </constructor-arg> </bean> <bean id="american" factory-bean="personFactory" factory-method="getPerson"> <constructor-arg> <value>ame</value> </constructor-arg> </bean></beans> 感谢楼上高人!最后一个问题!再请问您一下,您说的“BeingFactory”或“PersonFactory”要继承spring的哪个类吗?还是普通的工厂就可以? 偶觉得加入SPRING后.就没有必要写工厂的.SRPING本身就是一个大工厂.把这些类ExcelCollectServiceImpl在配制文件写一下不就OK了.简单最好. 不用的,他就是一般的类,这个类有一个getBean()方法,可以自己写啊,根据传入的参数不同,返回不同的实例. 比如有一个接口是person,personFactory的方法返回这个接口,如果参数是chi,就person chinese = new ChinesePerson(),return chinese,这个ChinesePerson是实现了person的接口. 我有别人写的一点源码,不懂的话给你 [email protected] Struts2如何检测用户是否已经登录 急!xfire异常 For input string: "" ssh中 怎么来进行统一异常 一个页面调用多个action? tapestry中IRequestCycle是什么类? 各位大哥大姐 ,求助!!1 小弟在jbuilder2006+weblogic中建立关系时遇到一个小问题!!!!!很急哈!!!! Java电子教程去哪找? 请问开源的项目是怎么赚钱的? 完全免费??? 这是在干什么? 如果通过ldap协议获得用户是否禁用的信息? 大家看一下啊 提点意见
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="excelDao" class="com.gwssi.datacollect.dao.impl.ExcelDataCollectDAOImpl">
<property name="template" ref="jdbcTemplate" />
</bean>excelDao 引用一个事先配置好的bean-“jdbcTemplage”,这样是固定的就引用那个“jdbcTemplate”bean!问题是,我需要根据传入的不同的参数引用不同的bean呀!
<constructor-arg>
<value>dog</value>
</constructor-arg>
<property name="msg">
<value>我是狗</value>
</property>
</bean> <bean id="cat" class="lee.BeingFactory" factory-method="getBeing">
<constructor-arg>
<value>cat</value>
</constructor-arg>
<property name="msg">
<value>我是猫</value>
</property>
</bean>有两个例子,你自己看看,根据传入的参数不同,生成不同的实例,工厂bean是BeingFactory,工厂方法是getBeing()
<beans> <bean id="personFactory" class="lee.PersonFactory"/> <bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
<constructor-arg>
<value>chin</value>
</constructor-arg>
</bean> <bean id="american" factory-bean="personFactory" factory-method="getPerson">
<constructor-arg>
<value>ame</value>
</constructor-arg>
</bean>
</beans>
SRPING本身就是一个大工厂.把这些类ExcelCollectServiceImpl在配制文件写一下不就OK了.简单最好.
不用的,他就是一般的类,这个类有一个getBean()方法,可以自己写啊,根据传入的参数不同,返回不同的实例.
比如有一个接口是person,personFactory的方法返回这个接口,如果参数是chi,就person chinese = new ChinesePerson(),return chinese,这个ChinesePerson是实现了person的接口.
我有别人写的一点源码,不懂的话给你
[email protected]