什么是依赖注入
- 依赖 : 指Bean对象的创建依赖于容器 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
依赖注入的类型有三类
- 基本数据类型和String类型
- 其他bean类型(在配置文件中或者注解配置过的bean)
- 复杂类型/集合类型
注入的方式有三种
- 使用构造函数注入
 构造函数方式注入:使用标签constructor-arg
- type:用于指定注入的数据类型
- index:用于指定注入的数据给构造函数中指定索引位置的参数赋值,索引位置是从0开始
- name:用于指定给构造函数中指定名称的参数赋值(推荐使用)
- value:用于提供基本类型和string类型数据赋值
- ref:用于指定其他的bean数据类型,是指在spring容器中出现过的bean对象,引用id进来
测试:
实体类
public class User {
    private String name;
    private Integer age;
    private Date date;
    public User(){
    }
    public User(String name, Integer age, Date date) {
        this.name = name;
        this.age = age;
        this.date = date;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }
}
Spring核心配置文件beans.xml
   <bean id="user" class="com.Dao.User">
        <constructor-arg name="name" value="chenhui"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
        <constructor-arg name="date" ref="date"></constructor-arg>
    </bean>
    <bean id="date" class="java.util.Date"></bean>
测试类:
 @Test
    public void test(){
        //通过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       //获取Spring容器中的bean对象,通过id和类字节码来获取
        User user = Context.getBean("user", User.class);
        System.out.println(user);
    }
结果:

- 使用set方法注入(常用)
- 使用标签property
- name:用于指定注入时所调用的set方法名称
- value:用于提供基本类型和string类型数据赋值
- ref:用于指定其他的bean数据类型,是指在spring容器中出现过的bean对象,引用id进来
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试:
实体类
public class User {
    private String name;
    private Integer age;
    private Date date;
    public User(){
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public void setDate(Date date) {
        this.date = date;
    }
}
Spring核心配置文件beans.xml
<!--    使用的是默认构造函数创建对象-->
    <bean id="user" class="com.Dao.User">
        <property name="name" value="chenhui"></property>
        <property name="age" value="22"></property>
        <property name="date" ref="date"></property>
     </bean>
    <bean id="date" class="java.util.Date"></bean>
测试类:
  @Test
    public void test(){
        //通过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       //获取Spring容器中的bean对象,通过id和类字节码来获取
        User user = Context.getBean("user", User.class);
        System.out.println(user);
    }
结果:

复杂类型/集合类型的注入
- 用于给list结构集合注入标签:
- list array set
- 用于给map结构集合注入的标签:
- map property
- 结构相同,标签可以互换,即标签体可以互换使用,不会报错
测试:
实体类:
public class User {
    private String[] strings;
    private List<String> list;
    private Map map;
    private Properties properties;
    private Set set;
    public void setStrings(String[] strings) {
        this.strings = strings;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public void setSet(Set set) {
        this.set = set;
    }
    public User(){
    }
    public void print(){
        System.out.println(Arrays.toString(strings));
        System.out.println(list);
        System.out.println(map);
        System.out.println(set);
        System.out.println(properties);
    }
}
Spring核心配置文件beans.xml
<bean id="user" class="com.Dao.User">
        <property name="strings">
            <array>
                <value>dasd</value>
                <value>aaaa</value>
                <value>dfff</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>dff</value>
                <value>hjjgj</value>
                <value>dgfhfgh</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>1213</value>
                <value>1fsdf</value>
                <value>trg</value>
            </set>
        </property>
        <property name="map">
            <map>
<!--                使用第一种方式-->
                <entry key="chenhui" value="123"></entry>
                <entry key="xie" value="123"></entry>
<!--                也可使用第二种方式-->
                <entry key="chen">
                    <value>333</value>
                </entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="cc">1</prop>
                <prop key="ww">2</prop>
                <prop key="dsa">3</prop>
            </props>
        </property>
    </bean>
测试类:
 @Test
    public void test(){
        //通过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       //获取Spring容器中的bean对象,通过id和类字节码来获取
        User user = Context.getBean("user", User.class);
        //调用方法使集合类型注入执行
        user.print();
    }
结果:

当结构相同,标签可以互换
<bean id="user" class="com.Dao.User">
        <property name="strings">
            <set>
                <value>1213</value>
                <value>1fsdf</value>
                <value>trg</value>
            </set>
        </property>
        <property name="list">
            <array>
                <value>dasd</value>
                <value>aaaa</value>
                <value>dfff</value>
            </array>
        </property>
        <property name="set">
            <list>
                <value>dff</value>
                <value>hjjgj</value>
                <value>dgfhfgh</value>
            </list>
        </property>
        <property name="map">
            <props>
                <prop key="cc">1</prop>
                <prop key="ww">2</prop>
                <prop key="dsa">3</prop>
            </props>
        </property>
        <property name="properties">
            <map>
                <!--                使用第一种方式-->
                <entry key="chenhui" value="123"></entry>
                <entry key="xie" value="123"></entry>
                <!--                也可使用第二种方式-->
                <entry key="chen">
                    <value>333</value>
                </entry>
            </map>
        </property>
    </bean>


结果:
[图片上传中...(image-85fd26-1613912046687-0)]










