文章目录
1、概念
所谓的依赖注入(Dependency Injection,DI),其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中。
- 依赖:指Bean对象的创建依赖于容器,Bean对象的依赖资源
- 注入:指Bean对象所依赖的资源,由容器来设置和装配
2、构造器注入
即《Hello-Spring》中的IOC创建对象方式。
3、set注入
Set注入顾名思义,被注入的属性需要有set方法, Set注入支持简单类型和引用类型,Setter注入时在bean实例创建完成后执行的。
- Address实体类
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 + '\'' +
'}';
}
}
- Student实体类
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 String wife;
private Properties info;
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 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
- 注入
<bean id="address" class="com.xbaozi.pojo.Address">
<property name="address" value="localhost"/>
</bean>
<bean id="student" class="com.xbaozi.pojo.Student">
<!-- 常量注入 -->
<property name="name" value="Spring"/>
<!-- Bean注入,通过ref和另一个bean的id引入bean -->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- List注入 -->
<property name="hobbies">
<list>
<value>搞外设</value>
<value>敲代码</value>
<value>改bug</value>
<value>戴假发</value>
</list>
</property>
<!-- Map注入 -->
<property name="card">
<map>
<entry key="学号" value="20220987612"/>
<entry key="专业号" value="0987"/>
</map>
</property>
<!-- Set注入 -->
<property name="games">
<set>
<value>LOL</value>
<value>GTA</value>
<value>DOTA</value>
</set>
</property>
<!-- Null注入 -->
<property name="wife">
<null/>
</property>
<!-- Properties注入 -->
<property name="info">
<props>
<prop key="Driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost/database</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
- 测试
@Test
public void test01() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
------- 测试结果由于太长cv出来放在了下面 --------
/* 从上到下分别为
常量注入
bean注入
数组注入
List注入
Map注入
Set注入
Null注入
Properties注入
*/
Student{
name='Spring',
address=Address{
address='localhost'
},
books=[红楼梦, 西游记, 水浒传, 三国演义],
hobbies=[搞外设, 敲代码, 改bug, 戴假发],
card={
学号=20220987612,
专业号=0987
},
games=[LOL, GTA, DOTA],
wife='null',
info={
password=123456,
url=jdbc:mysql://localhost/database,
Driver=com.mysql.jdbc.Driver,
username=root
}
}
4、其他注入
除了上述的几种注入方式之外,还有p命名注入和c命名注入。
- User实体类
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = 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 +
'}';
}
}
- P命名空间注入 : 需要在头文件中加入对应的约束文件,即下面代码中的第4行。要使用p命名空间注入要求无参构造必须存在,否则将会报错
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.xbaozi.pojo.User" p:age="18" p:name="Spring"/>
</beans>
- c命名空间注入:需要在头文件中加入约束文件,即下面代码中的第4行。要使用p命名空间注入要求无参构造必须存在,否则将会报错
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user01" class="com.xbaozi.pojo.User" c:name="Spring-c" c:age="18"/>
</beans>
- 测试
@Test
public void test01() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
5、bean作用域
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 。
其中,request、session作用域仅在基于web的应用中使用,只能用在基于web的Spring ApplicationContext环境。
5.1、Single
**Spring中的缺省作用域。**当一个bean的作用域为Singleton,那个String IOC容器中就只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则智慧返回bean的同一实例。Singleton是单例模式,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,它也存在了,每次获取到的对象都是同一个对象。
<bean id="user" class="com.xbaozi.pojo.User" scope="singleton">
当我们类中的无参构造器中做出一些标记如输出一句话时,debug下面的测试代码我们会发现,在执行完第一条语句的时候该无参构造方法便被调用了,并且最后输出的两者地址是相同的。
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
System.out.println(user==user2);
}
5.2、Prototype
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
<bean id="user01" class="com.xbaozi.pojo.User" scope="prototype">
<bean id="user02" class="com.xbaozi.pojo.User" singleton="false">
同样,当我们类中的无参构造器中做出一些标记如输出一句话时,debug下面的测试代码我们会发现,在执行完第一条语句的时候该无参构造方法并没有被调用,而是获取bean的时候才执行,并且最后输出的两者地址是不相同的。
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user01");
User user2 = (User) context.getBean("user02");
System.out.println(user==user2);
}
5.3、Request
当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。
5.4、Session
当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。