DI依赖注入

依赖注入

构造器注入

###set方式注入【重点】 依赖注入:set注入 - 依赖:bean对象的创建依赖于容器 - 注入:bean对象中的所有属性,由容器来注入

环境搭建

  1. 复杂类型
package com.god.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  1. 真实测试对象
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;
}
  1. 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="student" class="com.god.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="Dominus"/>

    </bean>
</beans>
  1. 测试类
import com.god.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    static void main() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student)context.getBean("student");
        System.out.println(student.getName());
    }
}

完善注入信息:

<?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="com.god.pojo.Address"/>

    <bean id="student" class="com.god.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="Dominus"/>
        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"/>
        <!--数组注入,array-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>寻梦环游记</value>
                <value>西游记</value>
            </array>
        </property>
        <!--List-->
        <property name="hobbys">
            <list>
                <value>足球</value>
                <value>棒球</value>
            </list>
        </property>
        <!--Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="123123122312312013"/>
                <entry key="银行卡" value="99999999999996"/>
            </map>
        </property>
        <!--Set-->
        <property name="games">
            <set>
                <value>洛克王国</value>
                <value>GTA</value>
            </set>
        </property>
        <!--null-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">666</prop>
                <prop key="身高">1.87</prop>
            </props>
        </property>
    </bean>
</beans>

拓展方式注入

可以使用p命名空间和c命名空间进行注入
<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"
       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">

    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.god.pojo.User" p:name="Zlatan" p:age="38"/>

    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.god.pojo.User" c:name="liuyf" c:age="18"/>
    
</beans>

测试:

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user.toString());
    }

注意点:p命名空间和c命名空间不能直接使用,需要导入约束
需要添加:

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

bean的作用域

屏幕截图 2026-02-21 112003

  1. 单例模式(Spring默认机制)
<bean id="user2" class="com.god.pojo.User" c:name="liuyf" c:age="18" scope="singleton"/>
  1. 原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user2" class="com.god.pojo.User" c:name="liuyf" c:age="18" scope="prototype"/>
  1. 其余的,request、session、application,这些只能在web开发中使用到
posted @ 2026-02-20 21:27  Dominus  阅读(1)  评论(0)    收藏  举报