Spring的依赖注入

Posted 可以说晚安了吗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的依赖注入相关的知识,希望对你有一定的参考价值。

spring依赖注入

1.构造器注入

2.Set方式注入

依赖注入:Set注入

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

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

package com.liqiliang.pojo;

import java.util.*;

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties 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 +
                ‘}‘;
    }

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

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

    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> 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 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;
    }
}

package com.liqiliang.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 + ‘‘‘ +
                ‘}‘;
    }
}

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.liqiliang.pojo.Address">
        <property name="address" value="上海"/>
    </bean>
    <bean id="student" class="com.liqiliang.pojo.Student">
        <!--普通值注入,value-->
        <property name="name" value="李启亮"/>

        <!--bean注入,ref-->
        <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="1"/>
                <entry key="性别" value="男"/>
                <entry key="年龄" value="18"/>
            </map>
        </property>
        
        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CF</value>
                <value>QQcar</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null/>
        </property>

        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">20200513</prop>
                <prop key="姓名">小李</prop>
                <prop key="driver">1</prop>
                <prop key="url">2</prop>
                <prop key="username">3</prop>
                <prop key="password">4</prop>
            </props>
        </property>
    </bean>
</beans>
import com.liqiliang.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("bean.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.toString());
        /*
        Student{
         name=‘李启亮‘,
         address=Address{address=‘上海‘},
         books=[红楼梦, 西游记, 三国演义],
         hobbys=[听课, 敲代码, 看电影],
         card={学号=1, 性别=男, 年龄=18},
         games=[LOL, CF, QQcar],
         wife=‘null‘,
         info={学号=20200513, password=4, url=2, driver=1, 姓名=小李, username=3}}
         */
    }
}

3.拓展方式注入(p命名空间和c命名空间)

实体类

package com.liqiliang.pojo;

public class User {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘‘‘ +
                ‘}‘;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--p命名和c命名必须要有上面的声名
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
	   并且实体类中要有构造器		-->

    <!--p命名空间注入 , 可以直接注入属性的值:property-->
    <bean id="user" class="com.liqiliang.pojo.User" p:name="李启亮" p:id="1"/>

    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.liqiliang.pojo.User" c:id="2" c:name="小李"/>

</beans>

测试类

import com.liqiliang.pojo.Student;
import com.liqiliang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test2(){
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("UserBean.xml");
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
    }

    @Test
    public void test3(){
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("UserBean.xml");
        User user = (User) applicationContext.getBean("user2");
        System.out.println(user);
    }
}

以上是关于Spring的依赖注入的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Android 片段和依赖注入

Android片段和依赖注入

Spring依赖注入的方式

spring依赖注入,和控制反转。用自己的话说是怎么说的。最好能够用代码来解释

Spring设置注入和构造注入的区别