首页 > 代码库 > Spring 参数注入

Spring 参数注入

一个(类)Bean可能包含多种属性,这些属性怎么配置???  见下:

   用People  Dog Cat Tiger  Panda五个类来学习一些常用的=_=  

   重点在XML

  

    <!--基本类型注入-->
    <bean id="tiger" class="party.infoo.entity.Tiger">
        <property name="name" value="Tiger_AA"/>
        <property name="age" value="3"/>
    </bean>
    <bean id="cat" class="party.infoo.entity.Cat">
        <property name="name" value="Cat_AA"/>
        <property name="age" value="2"/>
    </bean>
    <bean id="dog" class="party.infoo.entity.Dog">
        <property name="name" value="Dod_AA"/>
        <property name="age" value="5"/>
    </bean>
    <bean id="panda" class="party.infoo.entity.Panda">
        <property name="name" value="Panda_A"/>
        <property name="age" value="3"/>
    </bean>
    <!--参数注入方法介绍-->
    <bean id="people" class="party.infoo.entity.People">
        <!--基本类型注入-->
        <property name="name" value="People_AA"/>

        <!--bean注入-->
        <property name="dog" ref="dog"/>

        <!--级联属性注入《要求已经new过的》-->
        <property name="cat.name" value="Cat_BB"/>

        <!--内部bean注入-->
        <property name="tiger">
            <bean class="party.infoo.entity.Tiger">
                <property name="name" value="Tiger_BB"/>
                <property name="age" value="10"/>
            </bean>
        </property>

        <!--null值注入-->
        <property name="panda">
            <null></null>
        </property>

        <!--list-->
        <property name="books">
            <list>
                <value>JAVA</value>
                <value>ENGLISH</value>
            </list>
        </property>
        <!--set-->
        <property name="hobbies">
            <set>
                <value>唱歌</value>
                <value>玩游戏</value>
            </set>
        </property>
        <!--map-->
        <property name="works">
            <map>
                <entry>
                    <key><value>星期四</value></key>
                    <value>上班</value>
                </entry>
                <entry>
                    <key><value>星期五</value></key>
                    <value>放假</value>
                </entry>
            </map>
        </property>
        <!--proeperty-->
        <property name="address">
            <props>
                <prop key="addres1">AAAAAAAAAA</prop>
                <prop key="addres2">BBBBBBBBBB</prop>
            </props>
        </property>
    </bean>

 

 

package party.infoo.entity;

import java.util.*;

/**
 * Created by infoo on 2017/2/18.
 */
public class People {
  private String name;
  private Dog dog;
  private Cat cat=new Cat();
  private Tiger tiger;
  private Panda panda;
  private List<String> books=new ArrayList<String>();
  private Set<String> hobbies=new HashSet<String>();
  private Map<String,String> works=new HashMap<String,String>();
  private Properties address=new Properties();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Tiger getTiger() {
        return tiger;
    }

    public void setTiger(Tiger tiger) {
        this.tiger = tiger;
    }

    public Panda getPanda() {
        return panda;
    }

    public void setPanda(Panda panda) {
        this.panda = panda;
    }

    public List<String> getBooks() {
        return books;
    }

    public void setBooks(List<String> books) {
        this.books = books;
    }

    public Set<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(Set<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getWorks() {
        return works;
    }

    public void setWorks(Map<String, String> works) {
        this.works = works;
    }

    public Properties getAddress() {
        return address;
    }

    public void setAddress(Properties address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "name=‘" + name + ‘\‘‘ +
                ", dog=" + dog +
                ", cat=" + cat +
                ", tiger=" + tiger +
                ", panda=" + panda +
                ", books=" + books +
                ", hobbies=" + hobbies +
                ", works=" + works +
                ", address=" + address +
                ‘}‘;
    }
}
package party.infoo.entity;

/**
 * Created by infoo on 2017/2/18.
 */
public class Tiger {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Tiger{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}
package party.infoo.entity;

/**
 * Created by infoo on 2017/2/18.
 */
public class Panda {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Panda{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}
package party.infoo.entity;

/**
 * Created by infoo on 2017/2/18.
 */
public class Dog {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}
package party.infoo.entity;

/**
 * Created by infoo on 2017/2/18.
 */
public class Cat {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

Spring 参数注入