依赖注入spring

依赖:bean对象的创建依赖于容器

注入:bean对象中的所有属性,由容器来注入

下面咱们讲讲set注入各种类型

搭建测试环境

1,复杂类型

建立Address类,私有一个address变量,构造set和get方法,并且tostring一下

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

2,真实测试对象

建立Student类,把各种类型的变量都加入里面并且私有化,构造set和get方法,toString();

public class Student {
    private String name;
    private String address;
    private String[] books;
    private List<String>hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address='" + address.toString() + '\'' +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

3,配置beans.xml文件

普通注入,直接写value

Bean注入,写入ref,并且把两个类都引入

数组引入,用Array引入即可

list注入,<list><value>赋值</value></list>

Map注入,<map><entry  key=""  value=""></map>

Set注入<set><vaule>赋值</value></set>

Props注入  <prop key="">赋值</prop>

<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="address" class="com.yinggu.pojo.Address"/>
    <bean id="Student" class="com.yinggu.pojo.Student">
        <!--第一种注入,普通值注入-->
        <property name="name" value="李四"/>
        <!--Bean注入-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
            </array>
        </property>
        <!--list注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看电影</value>
                <value>打代码</value>
            </list>
        </property>
        <!--MAp注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="112321322"></entry>
                <entry key="银行卡" value="1223545322"></entry>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>BOB</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="driver">20220321033</prop>
                <prop key="url">男</prop>
                <prop key="root">小明</prop>
            </props>
        </property>
    </bean>
</Beans>

4,编写测试类

  public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("Student");
        System.out.println(student.toString());
    }

上一篇:ssm整合 注解版本


下一篇:基于ssm jsp在线鲜花销售系统源码和论文