1. 构造器注入

调用构造方法。

2. set方式注入【重点】

依赖注入:set注入!

  • 依赖:bean对象的创建依赖于容器!
  • 注入:bean对象中的所有属性,由容器来注入!

复杂类型

String,类(对象),数组,List,Map,Set,null,Properties等。

3. 扩展方式注入

我们可以使用p命名空间和c命名空间。

p命名空间注入,可以直接注入属性的值:property

<bean id="user" class="com.company.org.User" p:name="muxing" p:age="18"/>

c命名空间注入,通过构造器注入:construct-args

<bean id="user" class="com.company.org.User"c:name="muxing" c:age="18"/>

—————————————————————————————————————————————

下面写一个实例:

重点关注application.xml文件中,各种类型的注入方式。

project的文件结构

(1)Address.java

package com.company.org;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

(2)Student.java

package com.company.org;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    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 Address getAddress() {
        return address;
    }

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

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

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

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

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

    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 + '\'' + "\n" +
                " address=" + address.toString() + "\n" +
                " books=" + Arrays.toString(books) + "\n" +
                " hobbies=" + hobbies + "\n" +
                " card=" + card + "\n" +
                " games=" + games + "\n" +
                " wife='" + wife + '\'' + "\n" +
                " info=" + info +
                '}';
    }
}

(3)application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/beans ">

    <bean id="address" class="com.company.org.Address">
        <property name="address" value="北京"/>
    </bean>

    <bean id="student" class="com.company.org.Student">
        <!-- 第一种,普通值注入,value -->
        <property name="name" value="牧心"></property>

        <!-- 第二种,Bean注入, ref -->
        <property name="address" ref="address"/>

        <!-- 第三种,数组注入,  -->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>

        <!-- 第四种,List -->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!-- 第五种,map -->
        <property name="card">
            <map>
                <entry key="身份证" value="1234567890123456"/>
                <entry key="银行卡" value="1111111111111111"/>
            </map>
        </property>

        <!-- 第六种,set -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

        <!-- 第七种,null -->
        <property name="wife">
            <null/>
        </property>

        <!-- 第八种,prop -->
        <property name="info">
            <props>
                <prop key="学号">S123456</prop>
                <prop key="性别">男</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

    </bean>

</beans>

(4)MyTest.java

import com.company.org.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

运行结果:

Student{name='牧心'
 address=Address{address='北京'}
 books=[红楼梦, 西游记, 水浒传, 三国演义]
 hobbies=[听歌, 敲代码, 看电影]
 card={身份证=1234567890123456, 银行卡=1111111111111111}
 games=[LOL, COC, BOB]
 wife='null'
 info={password=123456, 学号=S123456, username=root, 性别=男}}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐