序:在上一章节中我们主要学习了IOC的概念和应用场景,以及初步了解了注解式依赖注入的使用方法,本章节我们继续深入学习IOC的XML注入方式和生命周期等相关概念。
第一节:Spring生命周期及依赖注入
一、生命周期:
首先,我们先看一下官网的描述:
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container......
由此看出,那什么是生命周期呢?我们不妨这样去理解它:
对于普通的 Java 对象,当 new 的时候创建对象,然后该对象就能够使用了。一旦该对象不再被使用,则由 Java 自动进行垃圾回收。而 Spring 中的对象是 bean,bean 和普通的 Java 对象没啥大的区别,只不过 Spring 不再自己去 new 对象了,而是由 IoC 容器去帮助我们实例化对象并且管理它,我们需要哪个对象,去问 IoC 容器要即可。IoC 其实就是解决对象之间的耦合问题,Spring Bean 的生命周期完全由容器控制。
生命周期的作用域:
-
singleton : 单例模式,使用 singleton 定义的 Bean 在 Spring 容器中只有一个实例,这也是 Bean 默认的作用域,Spring 中的 bean 默认都是单例的。
-
prototype : 原型模式,每次请求都会创建一个新的 bean 实例。
-
request : 每一次 HTTP 请求都会产生一个新的 bean,而对不同的 HTTP 请求,会返回不同的实例,该 bean 仅在当前 HTTP request 内有效。
-
session : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。
-
global-session:全局 session 作用域,它们基于 portlet 容器,可以像 servlet 一样处理 HTTP 请求。但是,与 servlet 不同,每个 portlet 都有不同的会话,该作用域仅在使用 portlet context 时有效。
对于springbean来说:
-
实例化 Instantiation
-
属性赋值 Populate
-
初始化 Initialization
-
销毁 Destruction
作用域的配置方式,通过scope来配置:
<bean id="person" class="com.xxx.Person" scope="singleton"/>
执行过程:
-
Resource定位(内容由开发者提供),解析XML配置或注解配置的类,得到BeanDefinition——》
-
BeanDefinition的载入,创建bean对象——》
-
BeanDefinition的注册,对数据进行填充(完成IOC的定义)——》
-
回调实现Aware接口(如BeanNameAware、BeanFactoryAware、ApplicationContextAware等接口的set方法)——》
-
调用BeanPostProcessor的初始化前方法——》
-
调用init方法——》
-
调用BeanPostProcessor的初始化后方法(这里会AOP)——》
-
完成Bean的初始化,使用Bean(生存期)——》
-
关闭容器调用DisposableBean中的destory方法,进行销毁。
第二节依赖注入实现过程:
概念:DI (Dependency Injection) 依赖注入,DI 是指在组件之间传递依赖关系的过程中,将依赖关系在容器内部进行处理,这样就不必在应用程序代码中硬编码对象之间的依赖关系,实现了对象之间的解耦合。在 Spring 中,DI 是通过 XML 配置文件或注解的方式实现的。它提供了三种形式的依赖注入:构造函数注入、Setter 方法注入和接口注入。
一、注入方法:
1、构造方法注入:在工具类中写入方法(这里以utils为例),代码如下:
package utils;
public class ResultMsg {
public String message;
public String msg(){
return message;
}
public ResultMsg(String message){
this.message = message;
}
}
在beanfactory中创建实例,代码如下:
<?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="helloWorld" class="utils.ResultMsg">
<constructor-arg index="0" value="good message"/>
</bean>
</beans>
调用测试,观察控制台:
package controller;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.ResultMsg;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMap