• 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的属性 -->
        <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的属性 -->
        <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>
            <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]]
  • 相关阅读:
    MySQL:如何查询出每个分组中的 top n 条记录?
    架构图解
    Jmeter(四十七)_性能测试统计超时率
    修改Docker容器启动配置参数
    Docker容器CPU、memory资源限制
    jmeter(四十六)参数化与断言实战
    jmeter(四十五)常用Beanshell脚本
    jmeter(四十四)常用性能指标分析
    linux性能监控常用命令
    jmap -heap 查看堆内存
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6880326.html
Copyright © 2020-2023  润新知