Spring学习一(入门和bean的作用域)

来源:互联网 发布:sql 查询包含字段的表 编辑:程序博客网 时间:2024/06/07 07:00

转载自:http://takeme.iteye.com/blog/2100726

第一




第二、Spring容器中Bean的作用域

1、全局属性

 default-lazy-init  :该beans下的所有bean 默认延迟初始化行为

default-merge:指定beans下的所有bean默认的mege行为

default-autowire:该beans下的所有bean都默认自动装配行为

default-autowire-candidates:指定beans下所有bean默认是否为自动装配的候选bean

default-init-method:指定beans下的所有bean的默认初始化方法

default-destory-method:指定beans下的所有的bean的默认回eton收方法

注:以上属性去掉default后就是单个bean的特性

2、bean的作用域:

  scope=singleton:默认值,单例模式

  scope=prototype:每次都会创建对象  ---->只用在web中的每一次回话需要一个唯一的值的情况

嵌套bean

<bean id="UserDaoService" class=""scope=prototype>

<property name="" value> </property>

<constructor-arg name="age" value="18" type="int"/>  

<!--内嵌bean->

 <property name="UserDao" <bean class=""/>  </property>

<property name="userdao1" ref=UserDao> </property>

 <ref local="userdao"/> 

</bean>

 3、详细使用举例

   <bean id=" " calss=" "  autowire-candidate ="false">  --此bean不参与自动装配

 <bean id=" " calss=" " autowire="no">  ---默认不在用自动注入

<bean id=" " calss=" "  autowire="byName">   --根据 setName()

<bean id=" " class=""  autowire="byType">   --根据 setName(Type type) 如果spring 容器中有多个 Type 的bean 会出异常 

<bean id=" " calss=" "  autowire="constructor">  ---根据类中的构造函数进行装配

<bean id=" " calss=" "  autowire="autodetect">  --用于2.5  3.0以后 交给spring ,决定是使用构造函数还是使用byType

4、给bean注入集合属性

  <bean id=""> calss="">

  <property name="">

  <list>

  <value=""></value>

 <value=""></value>

</list>

</property>

 </bean>

5、bean注入的原则:

  用构造方法注入时,要提供构造方法

  设值注入的bean,要提供setting注入,可以不提供getter方法


0 0
原创粉丝点击