首页 > 代码库 > Spring入门第四课

Spring入门第四课

注入参数详解:null值和级联属性

可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值。

和Struts,Hiberante等框架一样,Spring支持级联属性的配置。

可以如下设置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
    class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数
    id:表示容器的bean,id唯一 -->
    <bean id="hello" class="logan.spring.study.HelloWorld">
    </bean>
    
    <bean id="hello2" class="logan.spring.study.HelloWorld">
    </bean>
    
    <!-- 通过构造方法来配置Bean的属性 -->
    <bean id="car" class="logan.spring.study.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="Shanghai" index="1"></constructor-arg>
        <constructor-arg value="3000000" index="2" type="double"></constructor-arg>
    </bean>
    <bean id="car2" class="logan.spring.study.Car">
        <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
        <constructor-arg type="java.lang.String">
            <value><![CDATA[<Shanghai>]]></value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>250</value>
        </constructor-arg>
    </bean>
    
    <bean id="person" class="logan.spring.study.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="24"></property>
        <property name="car">
            <bean class="logan.spring.study.Car">
                <constructor-arg value="Ford"></constructor-arg>
                <constructor-arg value="Changan"></constructor-arg>
                <constructor-arg value="20000000" type="double"></constructor-arg>
            </bean>
        </property>
    </bean>
    
    <bean id="person2" class="logan.spring.study.Person">
        <constructor-arg value="Jerry"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
        <!-- 
        <constructor-arg ref="car"></constructor-arg>
         -->
         <constructor-arg><null/></constructor-arg>
    </bean>

</beans>

输出结果

五月 19, 2017 8:54:47 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 20:54:47 CST 2017]; root of context hierarchy
五月 19, 2017 8:54:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
HelloWorld‘s Constructor...
HelloWorld‘s Constructor...
Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=0]
Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]
Person [name=Jerry, age=25, car=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
    class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数
    id:表示容器的bean,id唯一 -->
    <bean id="hello" class="logan.spring.study.HelloWorld">
    </bean>
    
    <bean id="hello2" class="logan.spring.study.HelloWorld">
    </bean>
    
    <!-- 通过构造方法来配置Bean的属性 -->
    <bean id="car" class="logan.spring.study.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="Shanghai" index="1"></constructor-arg>
        <constructor-arg value="3000000" index="2" type="double"></constructor-arg>
    </bean>
    <bean id="car2" class="logan.spring.study.Car">
        <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
        <constructor-arg type="java.lang.String">
            <value><![CDATA[<Shanghai>]]></value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>250</value>
        </constructor-arg>
    </bean>
    
    <bean id="person" class="logan.spring.study.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="24"></property>
        <property name="car">
            <bean class="logan.spring.study.Car">
                <constructor-arg value="Ford"></constructor-arg>
                <constructor-arg value="Changan"></constructor-arg>
                <constructor-arg value="20000000" type="double"></constructor-arg>
            </bean>
        </property>
    </bean>
    
    <bean id="person2" class="logan.spring.study.Person">
        <constructor-arg value="Jerry"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
        <!-- 
        <constructor-arg ref="car"></constructor-arg>
        
        <constructor-arg><null/></constructor-arg>
        -->
        <constructor-arg ref="car"></constructor-arg>
        <property name="car.maxspeed" value="250"></property>
    </bean>

</beans>

下面是输出结果,注意:属性需要先初始化以后才可以为级联属性赋值,否则会有异常,和Struts2不同。

五月 19, 2017 9:17:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 21:17:51 CST 2017]; root of context hierarchy
五月 19, 2017 9:17:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
HelloWorld‘s Constructor...
HelloWorld‘s Constructor...
Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250]
Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]
Person [name=Jerry, age=25, car=Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250]]

 

Spring入门第四课