Spring入门系列之Ioc控制反转集合注入和Scope

x33g5p2x  于2021-03-14 发布在 Spring  
字(1.1k)|赞(0)|评价(0)|浏览(111)

Scope:
Singleton:每次拿到的bean都是一个bean
Prototype:每次拿到的都是一个新的对象。在Action层推荐用Prototype

在xml的bean配置中指定Scope属性即可:如下代码

<bean id="userService" class="com.spring.service.UserServiceImpl" scope="prototype">
		<property name="userDao" ref="userDao"></property>
</bean>

集合注入的案例:List、Set和Map

          <property name="someList">
               <list>
                       <value>a list element followed by a reference</value>
                       <ref bean="myDataSource"/>
               </list>
         </property>
         <property name="adminEmails">
                  <props>
                          <prop key="administrator">admin@some.org</prop>
                         <prop key="support">support@some.org</prop>
                  </props>
         </property>
          <property name="someMap">
                     <map>
                             <entry>
                                     <key><value>yup an entry</value></key>
                                             <value>just some string</value>
                               </entry>
                               <entry>
                                       <key><value>yup a ref</value></key>
                                             <ref bean="myDataSource">
                               </entry>
                      </map>
          </property>

相关文章