1.构造器注入


2.Set方式注入【重点】

依赖注入:Set注入!

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

    ​​​对应官网​

【环境搭建】

【Spring】依赖注入DI--Dependency Injection_User

1.pojo实体类

Address

package com.peach.pojo;

public class Address {
private String address;
}

Student

package com.peach.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;
}
2.分别注入
<?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.peach.pojo.Address">
<property name="address" value="北京"/>
</bean>

<bean id="student" class="com.peach.pojo.Student">
<!--第一种:普通值注入!-->
<!--<property name="name" value="peach"></property>-->
<property name="name">
<value>peach</value>
</property>

<!--第二种:Bean注入,ref 引用的是对象,而不是普通类型 -->
<property name="address" ref="address"/>

<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
</array>
</property>

<!--List注入-->
<property name="hobbys">
<list>
<value>跑步</value>
<value>听歌</value>
<value>看电影</value>
</list>
</property>

<!--Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>

<!--null注入 null(空值) -->
<property name="wife">
<null/>
</property>
<!--空串-->
<!--<property name="wife" value=""></property>-->

<!--两个比较特殊的 map 和 properties-->
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="123456"/>
<entry key="银行卡" value="123456"/>
</map>
</property>

<!--Properties-->
<property name="info">
<props>
<prop key="学号">1801</prop>
<prop key="姓名">小桃子</prop>
</props>
</property>

</bean>

</beans>
3.test类
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());
}
}

3.拓展方式注入

p命名和c命名注入

​官方文档​

【Spring】依赖注入DI--Dependency Injection_xml_02

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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">

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

<!--c命名空间注入,可以直接注入属性的值 construct-args -->
<bean id="user2" class="com.peach.pojo.User" c:name="peach22" c:age="18"/>
</beans>

【Spring】依赖注入DI--Dependency Injection_User_03


java的测试类

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

注意:
p命令和c命令不能直接使用,需要导入xml约束!

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

bean的作用域

【Spring】依赖注入DI--Dependency Injection_spring_04

  1. 单例模式(Spring默认机制)
<bean id="user2" class="com.peach.pojo.User" c:name="peach22" c:age="18" scope="singleton"/>

【Spring】依赖注入DI--Dependency Injection_User_05


【Spring】依赖注入DI--Dependency Injection_spring_06


【Spring】依赖注入DI--Dependency Injection_xml_07


2. 原型模式:每次从容器中get的时候,都会产生一个新对象!

<bean id="user2" class="com.peach.pojo.User" c:name="peach22" c:age="18" scope="prototype"/>

【Spring】依赖注入DI--Dependency Injection_spring_08


【Spring】依赖注入DI--Dependency Injection_xml_09


【Spring】依赖注入DI--Dependency Injection_spring_10


3. 其余的request、session、application这些只能在web开发中使用!