spring 依赖注入

238 阅读3分钟

代码

Student

package org.example.pojo;

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 Properties info;
    private String wife;

    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 Properties getInfo() {
        return info;
    }

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

    public String getWife() {
        return wife;
    }

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

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

Address

package org.example.pojo;

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 + '\'' +
                '}';
    }
}

User

package org.example.pojo;

public class User {
    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 "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Teacher

package org.example.pojo;

public class Teacher {
    private String name;
    private int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

teacherBeans.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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    c 命名空间注入,通过构造器注入 construct-args-->
    <bean id="teacher" class="org.example.pojo.Teacher" c:name="名字" c:age="30"/>
</beans>

userBeans.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    p 命名空间注入,可以直接注入属性值 property-->
    <bean id="user" class="org.example.pojo.User" p:name="名字" p:age="18"/>
</beans>

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="org.example.pojo.Address">
        <property name="address" value="美国"/>
    </bean>
    <bean id="student" class="org.example.pojo.Student">
        <!--        第一种:普通值注入,value-->
        <property name="name" value="测试名称"/>
        <!--        第二种:bean注入,ref-->
        <property name="address" ref="address"/>
        <!--        第三种:数组注入,ref-->
        <property name="books">
            <array>
                <value>《水浒传》</value>
                <value>《西游记》</value>
                <value>《红楼梦》</value>
                <value>《三国演义》</value>
            </array>
        </property>
        <!--        第四种:List注入-->
        <property name="hobbies">
            <list>
                <value>打游戏</value>
                <value>看电影</value>
            </list>
        </property>
        <!--        第五种:Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="5555523369774"/>
                <entry key="银行卡" value="5568823369774"/>
            </map>
        </property>
        <!--        第六种:Set注入-->
        <property name="games">
            <set>
                <value>overWatch</value>
                <value>crossFire</value>
            </set>
        </property>
        <!--        第七种:null 注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--        第八种:Properties 注入-->
        <property name="info">
            <props>
                <prop key="学号">1354</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>

MyTest

import org.example.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
        /*
        Student {
            name='测试名称',
            address=Address{address='美国'},
            books=[《水浒传》, 《西游记》, 《红楼梦》, 《三国演义》],
            hobbies=[打游戏, 看电影],
            card={身份证=5555523369774, 银行卡=5568823369774},
            games=[overWatch, crossFire],
            info={学号=1354, 性别=男},
            wife='null'
         }
         */
    }
    
        @Test
    public void uesrTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.toString());
    }
    
        @Test
    public void teacherTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("teacherBeans.xml");
        Teacher teacher = (Teacher) context.getBean("teacher");
        System.out.println(teacher.toString());
    }
}

注意点

p命名空间和c命名空间不能直接使用需要导入约束。

  • p
xmlns:p="http://www.springframework.org/schema/p"
  • c
xmlns:c="http://www.springframework.org/schema/c"