那你就在spring的配置文件里配置把
你的属性需要什么类型,就引用相应的bean <ref bean="" />

解决方案 »

  1.   

    那个“type”参数怎么让spring知道?
      

  2.   

    一般的不是这样吗,  <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呀!
      

  3.   

    与工厂模式相比Spring的ApplicationContext模式是更好的一种方式。在这里来说Spring相当于一个Bean容器,你将所有的Bean放入Spring中再根据bean id来获得。对于楼主这个例子,你有2种做法:第1种就是将你工厂中能够加工的bean分别配入Spring中。而id名就是你的type。这样你就可以根据通过的id来获得你不同的bean。第2种就是将你的工厂类配入Spring。在需要生产是,获取工厂类实例再获取生产出来的bean实例。lz仔细看下spring的reference的前几部分。你的理解有些偏差。
      

  4.   

    请问“pizzame”,您能否详细演示一下您说的那两种方法?小弟对spring实在是不熟,虚心向您请教!
      

  5.   

    <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>
      

  6.   

    感谢楼上高人!最后一个问题!再请问您一下,您说的“BeingFactory”或“PersonFactory”要继承spring的哪个类吗?还是普通的工厂就可以?
      

  7.   

    偶觉得加入SPRING后.就没有必要写工厂的.
    SRPING本身就是一个大工厂.把这些类ExcelCollectServiceImpl在配制文件写一下不就OK了.简单最好.
      

  8.   


    不用的,他就是一般的类,这个类有一个getBean()方法,可以自己写啊,根据传入的参数不同,返回不同的实例.
     比如有一个接口是person,personFactory的方法返回这个接口,如果参数是chi,就person chinese = new ChinesePerson(),return chinese,这个ChinesePerson是实现了person的接口.
     我有别人写的一点源码,不懂的话给你
     [email protected]