spring4 注入参数

 

 

 

<?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="people1" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
    </bean>
    <!--注入bean -->
    <bean id="dog1" class="com.java1234.entity.Dog">
        <property name="name" value="Jack"></property>
    </bean>
    
    <bean id="people2" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog" ref="dog1"></property>
    </bean>
    <!--内部bean -->
    <bean id="people3" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
            <bean class="com.java1234.entity.Dog">
                <property name="name" value="Tom"></property>
            </bean>
        </property>
    </bean>
    <!-- null值-->
    <bean id="people4" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
            <null></null>
        </property>
    </bean>
    <!-- 级联属性-->
    <!-- <bean id="people5" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog.name" value="Jack2"></property>
    </bean> -->
    <!-- 集合属性-->
    <bean id="people6" class="com.java1234.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog" ref="dog1"></property>
        <property name="hobbies">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <property name="loves">  <!--set是不可以重复的 -->
            <set>
                <value>唱歌2</value>
                <value>跳舞2</value>
            </set>
        </property>
        <property name="works">  <!--键值对 -->
            <map>
                <entry>
                    <key><value>上午</value></key>
                    <value>写代码</value>
                </entry>
                <entry>
                    <key><value>下午</value></key>
                    <value>测试代码</value>
                </entry>
            </map>
        </property>
        <property name="addresses"> <!--属性 一般用于系统配置 -->
            <props>
                <prop key="address1">aaaaa</prop>
                <prop key="address2">bbbbb</prop>
            </props>
        </property>
    </bean>
    
</beans>

private List<String> hobbies=new ArrayList<String>();
private Set<String> loves=new HashSet<String>();
private Map<String,String> works=new HashMap<String,String>();
private Properties addresses=new Properties();

posted @ 2017-06-15 16:10  海蓝7  阅读(281)  评论(0编辑  收藏  举报