第四讲:4.2 Spring注入参数

一,Spring注入参数-bean
1,在com.cruise.entity包下新建Dao类,属性name,

package com.cruise.entity;

public class Dog {

    private String name;

    public String getName() {
       return name;
    }

    public void setName(String name) {
       this.name = name;
    }
}
2,修改People属性,增加Dog属性,重新生成get 和set 方法,修改toString()方法,
  @Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";
    }
3,在beans.xml中定义id为dog1,注入属性值。定义id为people2,注入属性值,
<bean id="dog1" class="com.cruise.entity.Dog">
    <property name="name" value="Jack">property>
bean>

<bean id="people2" class="com.cruise.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>
4,Test类中添加测试方法test2(),运行-测试
@Test
    public void test2() {
       People people2 = (People)CPXAC.getBean("people2");
       System.out.println(people2);
    }
二,Spring注入参数-内部bean
1,在beans.xml中添加代码,定义people3,注入属性值,添加内部bean,
<bean id="people3" class="com.cruise.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.cruise.entity.Dog">
           <property name="name" value="Tom">property>
       bean>
    property>
bean>
2,在Test中添加测试方法test3(),运行-测试
@Test
    public void test3() {
       People people3 = (People)CPXAC.getBean("people3");
       System.out.println(people3);
    }
三,Spring注入参数-null标签bean
1,在beans.xml中添加代码,定义people4,注入属性值null标签,添加内部bean,
<bean id="people4" class="com.cruise.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>
3,修改People类中的toString()方法,否则会报空指针异常
@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + "]";
    }
2,在Test中添加测试方法test4(),运行-测试
@Test
    public void test4() {
       People people3 = (People)CPXAC.getBean("people4");
       System.out.println(people3);
    }
四,Spring注入参数-级联属性参数
1,在beans.xml中添加代码,定义people5,注入属性值,添加内部bean,
<bean id="people5" class="com.cruise.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>
2,修改People类中的Dog属性,修改toString,
private Dog dog=new Dog();
@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";
    }
3,在Test中添加测试方法test5(),运行-测试,测试之后还原People类dog属性写法,注释掉beans.xml中的people5.以防干扰。
@Test
    public void test5() {
       People people5 = (People)CPXAC.getBean("people5");
       System.out.println(people5);
    }
五,Spring注入参数-集合List属性参数
1,修改People类中属性,增加一个集合属性。重新生成toString()方法,get set 方法,
private List<String> hobbies = new ArrayList<String>();

@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies + "]";
    }
2,在beans.xml中添加代码,定义people6,注入属性值,添加内部bean,将id为people5的bean注释掉,
<bean id="dog1" class="com.cruise.entity.Dog">
    <property name="name" value="Jack"></property>
</bean>
  ……
<bean id="people6" class="com.cruise.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="hoggies">
       <list>
           <value>唱歌</value>
           <value>跳舞</value>
       </list>
    </property>
</bean>
3,在Test中添加测试方法test6(),运行-测试,
@Test
    public void test6() {
       People people5 = (People)CPXAC.getBean("people6");
       System.out.println(people5);
    }
六,Spring注入参数-集合Set属性参数
1,修改People类中属性,增加一个Set集合属性。重新生成toString()方法,get set 方法,
private Set<String> loves = new HashSet<String>();

@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies
              + ", loves=" + loves + "]";
    }
2,在beans.xml中添加代码,定义people7,注入属性值,添加内部bean,
<bean id="dog1" class="com.cruise.entity.Dog">
    <property name="name" value="Jack"></property>
</bean>
  ……
<bean id="people7" class="com.cruise.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="hoggies">
       <list>
           <value>唱歌</value>
           <value>跳舞</value>
       </list>
    </property>
  <property name="loves">
       <set>
           <value>唱歌2</value>
           <value>跳舞2</value>
       </set>
    </property>
</bean>
3,在Test中添加测试方法test7(),运行-测试,
@Test
    public void test7() {
       People people7 = (People)CPXAC.getBean("people7");
       System.out.println(people7);
    }
七,Spring注入参数-集合Map属性参数
1,修改People类中属性,增加一个Map集合属性。重新生成toString()方法,get set 方法,
private Map<String,String> works = new HashMap<String,String>();

@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies
              + ", loves=" + loves + ", works=" + works + "]";
    }
2,在beans.xml中添加代码,定义people8,注入属性值,添加内部bean,
<bean id="dog1" class="com.cruise.entity.Dog">
    <property name="name" value="Jack"></property>
</bean>
  ……
<bean id="people8" class="com.cruise.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>
           <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>
</bean>
3,在Test中添加测试方法test8(),运行-测试,
@Test
    public void test8() {
       People people7 = (People)CPXAC.getBean("people8");
       System.out.println(people7);
    }
八,Spring注入参数-集合Properties属性参数
1,修改People类中属性,增加一个集合属性。重新生成toString()方法,get set 方法,
private Properties addresses = new Properties();

@Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies
              + ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]";
    }
2,在beans.xml中添加代码,定义people9,注入属性值,添加内部bean,
<bean id="dog1" class="com.cruise.entity.Dog">
    <property name="name" value="Jack"></property>
</bean>
  ……
<bean id="people9" class="com.cruise.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>
           <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="地址1">北京</prop>
           <prop key="地址2">江苏</prop>
       </props>
    </property>
</bean>

3,在Test中添加测试方法test9(),运行-测试,
 @Test
    public void test9() {
       People people7 = (People)CPXAC.getBean("people9");
       System.out.println(people7);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值