Spring IOC容器赋值

ioc.xml

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

    <bean id="person1" class="com.kerwin.bean.Person">
        <property name="id" value="6"></property>
        <property name="name" value="zhangsan6"></property>
        <property name="age" value="26"></property>
        <property name="gender" value="男6"></property>

        <!--<property name="hobbies" value="book,girl,movie"></property>-->
        <property name="hobbies">
            <array>
                <value>book</value>
                <value>girl</value>
                <value>movie</value>
            </array>
        </property>

        <property name="address" ref="address"></property>

        <!--<property name="lists" value="1,2,3"></property>-->
        <property name="lists">
            <list>
                <bean id="address2" class="com.kerwin.bean.Address">
                    <property name="province" value="北京"></property>
                    <property name="city" value="1"></property>
                </bean>
                <bean class="com.kerwin.bean.Address">
                    <property name="province" value="上海"></property>
                    <property name="city" value="2"></property>
                </bean>
                <ref bean="address"></ref>
            </list>
        </property>

        <property name="sets">
            <set>
                <value>zhangsan</value>
                <value>zhangsan</value>
                <value>lisi</value>
            </set>
        </property>
        <property name="maps">
            <map>
                <entry key="a" value="aaa"></entry>
                <entry key="address" value-ref="address"></entry>
                <entry key="address2">
                    <bean class="com.kerwin.bean.Address">
                        <property name="province" value="广东省"></property>
                    </bean>
                </entry>
                <entry>
                    <key>
                        <value>heihei</value>
                    </key>
                    <value>haha</value>
                </entry>
                <entry key="list">
                    <list>
                        <value>11</value>
                        <value>22</value>
                    </list>
                </entry>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="111">aaa</prop>
                <prop key="222">bbb</prop>
            </props>
        </property>
    </bean>

    <bean id="address" class="com.kerwin.bean.Address" depends-on="person"></bean>
    <bean id="person" class="com.kerwin.bean.Person"></bean>
</beans>
public class Person {
    private int id;
    private String name;
    private Integer age;
    private String gender;
    private String[] hobbies;
    private Address address;

    private List<Address> lists;
    private Set<String> sets;
    private Map<String, Object> maps;
    private Properties properties;
}
public class Address {
    private String province;
    private String city;
    private String town;
}
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
Person person = context.getBean("person1", Person.class);
System.out.println(person6);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值