1. Bean的作用域
Bean作用域定义了对象实例在应用程序中的生命周期和访问范围,⽐如 singleton 单例作⽤域,就
 表示 Bean 在整个 Spring 中只有⼀份,它是全局共享的,那么当其他⼈修改了这个值之后,那么另⼀
 个⼈读取到的就是被修改的值
Spring 容器在初始化⼀个 Bean 的实例时,同时会指定该实例的作⽤域。Spring有 6 种作⽤域,最后
 四种是基于 Spring MVC ⽣效的:
- singleton:单例作⽤域
- prototype:原型作⽤域(多例作⽤域)
- request:请求作⽤域
- session:回话作⽤域
- application:全局作⽤域
- websocket:HTTP WebSocket 作⽤域
注意后 4 种状态是 Spring MVC 中的值,在普通的 Spring 项⽬中只有前两种
1.1 singleton
-  官⽅说明: 
 (Default) Scopes a single bean definition to a single object instance for eachSpring IoC container.
-  描述: 
 该作⽤域下的Bean在IoC容器中只存在⼀个实例:获取Bean(即通过
 applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是同⼀个对
 象。
-  场景:Bean对象的属性不需要更新 
-  备注:Spring默认选择该作⽤域 
下面演示单例作用域:
 
将User类设置为单例作用域

 在UserService类中,注入对象

测试类:
 
 输出:

1.2 prototype
- 官⽅说明:
 Scopes a single bean definition to any number of object instances.
- 描述:
 每次对该作⽤域下的Bean的请求都会创建新的实例:获取Bean(即通过
 applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是新的对象
 实例。
- 场景:通常有状态的Bean使⽤该作⽤域
下面演示prototype作用域的效果:


 
 测试类:
 
 
输出的结果:

1.3 request
- 官⽅说明:
 Scopes a single bean definition to the lifecycle of a single HTTP request. That is,
 each HTTP request has its own instance of a bean created off the back of a single bean
 definition. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:每次http请求会创建新的Bean实例,类似于prototype
- 场景:⼀次http的请求和响应的共享Bean
- 备注:限定SpringMVC中使⽤
1.4 session
- 官⽅说明:
 Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in
 the context of a web-aware Spring ApplicationContext.
- 描述:在⼀个http session中,定义⼀个Bean实例
- 场景:⽤户会话的共享Bean, ⽐如:记录⼀个⽤户的登陆信息
- 备注:限定SpringMVC中使⽤
1.5 application
- 官⽅说明:
 Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in
 the context of a web-aware Spring ApplicationContext.
- 描述:在⼀个http servlet Context中,定义⼀个Bean实例
- 场景:Web应⽤的上下⽂信息,⽐如:记录⼀个应⽤的共享信息
- 备注:限定SpringMVC中使⽤
1.6 websocket
- 官⽅说明:
 Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the
 context of a web-aware Spring ApplicationContext.
- 描述:在⼀个HTTP WebSocket的⽣命周期中,定义⼀个Bean实例
- 场景:WebSocket的每次会话中,保存了⼀个Map结构的头信息,将⽤来包裹客户端消息头。第⼀
 次初始化后,直到WebSocket结束都是同⼀个Bean。
- 备注:限定Spring WebSocket中使⽤
1.7设置Bean对象的作用域
方式1:直接设置作用域
 
方式二:使用枚举
 
注意:@Scope可以修饰类,也可以修饰方法。

 ElementType.Type的具体含义
 
2. Bean的生命周期
Bean的生命周期就是一个对象的声明周期,指的是从对象产生到销毁的一个过程。
 就像我们人一样,都会经历从出生到死亡的过程。Bean的生命周期如下:
-  实例化 
 实例化只为Bean分配了内存空间,这时候Bean中的属性都是默认值。
-  属性设置 
 将Bean中需要对象注入的属性设置好。
-  初始化 Bean
- 执行实现了各种 Aware通知的⽅法,
 如BeanNameAware、BeanFactoryAware、 ApplicationContextAware的接⼝⽅法;
- 执⾏ BeanPostProcessor初始化前置⽅法
- 执⾏@PostConstruct初始化⽅法,依赖注⼊操作之后被执⾏;
- 执⾏⾃⼰指定的init-method⽅法(如果有指定的话)
- 执⾏ BeanPostProcessor初始化后置⽅法
- 使用Bean
- 销毁Bean
下面代码模拟Bean的生命周期,只演示初始化、使用Bean、销毁Bean这三个过程。
 如下:
BeanLifeTest类
- 实现了BeanNameAware接口,重写了setBeanName方法
- 指定了@PostConstruct修饰的方法postContruct。
- 指定了@PreDestroy修饰的方法preDes。
- 一个自定义的useBean方法

MyPostProcessor类
- 实现了BeanPostProcessor接口
- 重写了前置初始化方法postProcessBeforeInitialization
- 重写了后置初始化方法postProcessAfterInitialization

预期的执行顺序:
 初始化:
 执行setBeanName方法 -> 执行postProcessBeforeInitialization初始化前置方法
 -> 执行postContruct方法-> 执行postProcessAfterInitialization初始化后置方法
使用Bean:
 执行useBean方法,打印使用Bean
销毁Bean:
 执行preDes方法,打印执行preDestroy方法
测试类:

 上下文对象一创建,spring就会立马去将Bean对象实例化,然后进行对象注入,并执行初始化的方法。
 创建完上下文对象后,获取到Bean对象,执行useBean方法
 最后调用上下文对象的destroy方法,此时会执行Bean对象中被@PreDestroy注解修饰的方法
输出结果:
