码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习篇02-Bean的xml配置

时间:2015-09-26 10:36:20      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

1.bean标签

  在Spring中,要在xml中配置一个bean,必须使用bean标签这个标签包含了要两个重要的属性:

  id:对bean进行标记在容器中是唯一的

  class:bean的类类型

  scope:表示该bean的实例个数,默认为单例。

2.property标签

  property是bean的子标签,通过它可以向bean实例的属性进行注入,重要属性如下:

    name:对应类的属性

    value:要注入类属性的原始值

    ref:要注入类属性的引用bean的id

     property里面可以包含bean的标签,这样property下的bean就是内部bean,内部bean只能在内部使用,外部是不能引用内部bean的。

  property的子标签可以是集合标签,这些集合标签包括list,map,properties等,其具体的使用情况可以参考Spring的帮助文档。

3.constructor-arg标签

   对bean对象进行构造注入时要用到的。

4.null标签

5.特殊字符

  <![CDATA[文本内容]]>

6.下面就是例子:

 

 

  

 

<!--通过全类名来配置Bean-->
<bean  id="person1" class="com.lkj.study.beanConfigs.Person">
    
</bean>

<!--通过属性设置方法进行属性注入-->
<bean  id="person2" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <property name="name" value="张三"></property>
    <property name="age">
        <value>123</value>
    </property>
</bean>

<!--通过构造方法进行属性注入-->
<bean  id="person3" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <constructor-arg value="zhangsan"></constructor-arg>
    <constructor-arg value="152"></constructor-arg>
</bean>


<bean  id="person4" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <constructor-arg ><null></null></constructor-arg>
    <constructor-arg value="152"></constructor-arg>
</bean>

特殊字符的注入
<bean  id="person5" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <constructor-arg >
    <value><![CDATA[<null>]]></value>
    </constructor-arg>
    <constructor-arg value="152"></constructor-arg>
</bean>

bean的引用;内部Bean是不能被外部引用的
<bean  id="person6" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <property name="personID" ref="personID1"></property>
    <property name="personID.name" value="auti"></property>
</bean>

<bean  id="person7" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <property name="personID" ref="personID1"></property>
    <property name="personID.name" value="auti"></property>
</bean>

<bean id="personID1" class="com.lkj.study.beanConfigs.PersonID">
    <property name="name" value="baoma"></property>
    <property name="price" value="120000"></property>
</bean>

<bean id="car1" class="com.lkj.study.beanConfigs.Car">
    <property name="name" value="baoma"></property>
    <property name="price" value="120000"></property>
</bean>

<bean id="car2" class="com.lkj.study.beanConfigs.Car">
    <property name="name" value="baoma"></property>
    <property name="price" value="120000"></property>
</bean>

<bean  id="person8" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <property name="cars">
        <list>
            <ref bean="car1"/>
            <ref bean="car2"/>
        </list>
    </property>
</bean>

<bean  id="person9" class="com.lkj.study.beanConfigs.Person" scope="prototype" >
    <property name="cars" ref="cars1">
        
    </property>
</bean>


<util:list id="cars1">
        <ref bean="car1"/>
        <ref bean="car1"/>
            <ref bean="car2"/>
</util:list>

Spring学习篇02-Bean的xml配置

标签:

原文地址:http://www.cnblogs.com/kejian/p/4840347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!