Spring框架之Day02为各类属性赋值

各类属性赋值

通过p名称空间为Bean赋值

	<bean id="person05" class="com.lwt.bean.Person" p:age="18" p:lastName="威彤" p:gender="" p:email="lwt@qq.com">

    </bean>

</beans>
@Test
public void test5() {
    Person bean = ioc.getBean("person05/04/03/02/01", Person.class);
    System.out.println(bean);
}

测试使用null

为默认的固定值再赋真正的null值

bean id="person01" class="com.lwt.bean.Person">
    <property name="lastName">
        <null></null>
    </property>
</bean>

引用类型赋值

ref引用外部其他bean 和 在property中引用内部bean

以下是bean中套property,通过bean Id=car01获取car对象值

<!--    bean套property-->
    <bean id="car01" class="com.lwt.bean.Car">
        <property name="carName" value="宝马"></property>
        <property name="color" value="白色"></property>
        <property name="price" value="300000"></property>
    </bean>
@Test
public void test1() {
    Person bean = ioc.getBean("person01", Person.class);
    System.out.println(bean);
    System.out.println("修改前"+bean.getCar());
    Car car = bean.getCar();
    Car car01 = (Car)ioc.getBean("car01");
    System.out.println(car01 == car);
    car.setCarName("自行车");
    System.out.println("修改后"+car01);
    System.out.println(car01 == car); // true 同时修改 同时改变 所以仍保持相同

}

结果:
Person{lastName=‘null’, age=null, gender=‘null’, email=‘null’, car=Car{carName=‘宝马’, price=300000, color=‘白色’}, books=null, maps=null, properties=null}
修改前Car{carName=‘宝马’, price=300000, color=‘白色’}
true
修改后Car{carName=‘自行车’, price=300000, color=‘白色’}
true

集合类型赋值

List

<!--           在<list>中使用<ref bean=id>-->
<bean id="book01" class="com.lwt.bean.Book">
        <property name="bookName" value="spring三刷"></property>
    </bean>
    <bean id="person01" class="com.lwt.bean.Person">
        <property name="books">
<!--            books = new ArrayList<Book>();-->
            <list>
                <bean id="xbook001x" class="com.lwt.bean.Book" p:author="彤儿" p:bookName="spring从入门到二刷"></bean>
                <ref bean="book01"></ref>
            </list>
        </property>

Map

<property name="maps">
<!--            maps = new LinkedHashMap<>();-->
            <map>
<!--                一个entry代表一个键值对-->
                <entry key="key1" value="value1"></entry>
                <entry key="key2" value="value2"></entry>
                <entry key="key3" value-ref="book01"></entry>
                <entry key="key4">
                    <bean class="com.lwt.bean.Car">
                        <property name="carName" value="机车女孩的车"></property>
                    </bean>
                </entry>
                <entry key="key5">
                    <map>
                        <entry key="key51" value="嵌套map"></entry>
                    </map>
                </entry>

            </map>
        </property>

@Test
public void test2() {
    Person bean = (Person) ioc.getBean("person01");
    Map<String, Object> maps = bean.getMaps();
    System.out.println(maps);
    System.out.println(bean.getProperties());
}

Properties

<property name="properties">
<!--            properties = new Properties();k=v都是string-->
            <props>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>
@Test
public void test2() {
    Person bean = (Person) ioc.getBean("person01");
    Map<String, Object> maps = bean.getMaps();
    System.out.println(maps);
    System.out.println(bean.getProperties());
}

util名称空间创建集合类型bean

util名称空间创建集合类型的bean:方便引用map,list,properties等

在properties中使用ref引用

<bean id="person02" class="com.lwt.bean.Person">
        <property name="maps" ref="myMap"></property>
</bean>
<!--        new LinkedHashMap<>();-->
    <util:map id="myMap">
        <entry key="key1" value="value1"></entry>
        <entry key="key2" value="value2"></entry>
        <entry key="key3" value-ref="book01"></entry>
        <entry key="key4">
            <bean class="com.lwt.bean.Car">
                <property name="carName" value="机车女孩的车"></property>
            </bean>
        </entry>
        <entry key="key5">
            <map>
                <entry key="key51" value="嵌套map"></entry>
            </map>
        </entry>
    </util:map>

<!--    List集合中有四个元素[[],Person,666,{}]-->
    <util:list id="myList">
        <list></list>
        <bean class="com.lwt.bean.Person"></bean>
        <value>666</value>
        <ref bean="myMap"></ref>
    </util:list>
</beans>

@Test
public void test3() {
    Person bean = (Person) ioc.getBean("person02");
    Map<String, Object> maps = bean.getMaps();
    System.out.println(maps);

    Map<String, Object> myMap = (Map<String, Object>) ioc.getBean("myMap");
    System.out.println(myMap.getClass());
}

结果 :
{key1=value1, key2=value2, key3=Book{bookName=‘spring三刷’, author=‘null’}, key4=Car{carName=‘机车女孩的车’, price=null, color=‘null’}, key5={key51=嵌套map}}
class java.util.LinkedHashMap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值