Spring之数据装配

1、概念

为bean中的属性注入值,成为数据的装配,可以装配不同类型的值,如int、Map、Boolean等。

2、简单类型(共19种) 

八种基本类型及包装类

byte  short  int   long  double float  char      boolean

Byte Short Integer Long  Double Float  Character Boolean

String Class Resource

简单类型装配值  -- 使用value属性装配

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="springBean" class="ioc04.SpringBean">
            <property name="age" value="13"/>
            <property name="bt" value="22"/>
            <property name="price" value="12.2"/>
            <property name="username" value="tom"/>
            <property name="clazz" value="java.lang.String"/>
        </bean>
</beans>

3、集合类型

数组 List Set Map Properties

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean name="otherBean" class="ioc05.OtherBean">
            <property name="name" value="tom"/>
        </bean>
        <bean name="springBean" class="ioc05.SpringBean">
            <!-- 引用IOC容器中另外一个bean  使用ref属性 -->
            <property name="otherBean" ref="otherBean"/>
            <property name="arrays">
                <!--Array使用array装配-->
                <array>
                    <value>1</value>
                    <value>2</value>
                    <value>4</value>
                </array>
                <!--Array也可以使用List装配-->
                <!--<List>
                    <value>1</value>
                    <value>2</value>
                    <value>4</value>
                </List>-->
            </property>
            <property name="lists">
                <list>
                    <!-- 由于List中放的是OtherBean,所以这里不可以用value属性赋值   因改用ref -->
                    <!--使用IOC中已经存在的OtherBean-->
                    <ref bean="otherBean"/>
                    <!--List中元素可重复-->
                    <ref bean="otherBean"/>
                    <!-- 重新创建一个新的OtherBean-->
                    <bean class="ioc05.OtherBean">
                        <property name="name" value="lokus"/>
                    </bean>
                </list>
            </property>
            <property name="sets">
                <set>
                    <!--Set中元素不可重复 如下两个ref引用IOC容器的同一个实例,因此在Set中只会有一个值-->
                    <ref bean="otherBean"/>
                    <ref bean="otherBean"/>
                    <!-- 重新创建一个新的OtherBean-->
                    <bean class="ioc05.OtherBean">
                        <property name="name" value="zhangsan"/>
                    </bean>
                </set>
            </property>
            <property name="map" >
                <map>
                    <entry key-ref="otherBean" value="java.lang.Integer"/>
                </map>
            </property>
            <property name="properties">
                <props>
                    <!--properties中都是字符串-->
                    <prop key="key1">value1</prop>
                </props>
            </property>
        </bean>
</beans>

map集合在装配时有简单类型、引用类型两种方法

4、其他bean的引用

IOC容器要注入的值,是另外一个bean的值 ---->使用ref

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean name="otherBean" class="ioc05.OtherBean">
            <property name="name" value="tom"/>
        </bean>
        <bean name="springBean" class="ioc05.SpringBean">
            <!-- 引用IOC容器中另外一个bean  使用ref属性 -->
            <property name="otherBean" ref="otherBean"/>
        </bean>
</beans>

5、NULL类型

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="springBean" class="ioc06.SpringBean">
            <!--将name属性装配成null-->
            <property name="name">
                <null/>
            </property>
        </bean>
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值