Spring 参数注入

Posted Lucky、Dog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 参数注入的主要内容,如果未能解决你的问题,请参考以下文章

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?

Spring属性注入构造方法注入工厂注入以及注入参数(转)

常见的三种注解注入方式对比

Spring MVC方法参数注入

Java 求spring用@Autowired进行方法参数注入例子。