二、Ioc控制反转集合注入和Scope
最后更新于:2022-04-01 11:38:08
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>
~~~