目录
现在有一个公共的 Bean,通过给 A 用户 和 B 用户使用, 然后在使用的过程中 A 偷偷的修改了公共 Bean 的数据, 导致 B 在使用时发生了预期之外的逻辑错误
(1) 公共 Bean: [名称是 java]
- @Component
- public class Users {
- @Bean
- public User user1() {
- User user = new User();
- user.setId(1);
- user.setName("java");
- return user;
- }
- }
(2) A 用户使用时,进行了修改操作 [名称是 悟空, 进行了修改操作]
- @Controller
- public class BeanScopesController {
-
- @Autowired
- private User user1;
-
- public User getUser1() {
- User user = user1;
- System.out.println("Bean 原 name: " + user.getName());
- user.setName("悟空");
- return user;
- }
- }
(3) B 用户再去使用公共 Bean 的时候
- @Controller
- public class BeanScopesController2 {
-
- @Autowired
- private User user1;
-
- public User getUser1() {
- User user = user1;
- return user;
- }
- }
(4) 打印 A 用户和 B 用户公共 Bean 的值
- public class BeanScopesTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
-
- BeanScopesController beanScopesController = context.getBean(BeanScopesController.class);
- System.out.println("A 对象修改之后 Name: " + beanScopesController.getUser1().toString());
-
- BeanScopesController2 beanScopesController2 = context.getBean(BeanScopesController2.class);
- System.out.println("B 对象读取到是 Name: " + beanScopesController2.getUser1().toString());
- }
- }
(5) 运行程序

(6) 原因分析
存在这个问题的原因就是 Bean 默认情况下是 单例模式 (singleton), 也就是所有类使用的都是同一个对象, 使用同一个对象,那就是没有过多的生命周期的创建和销毁,从而可以提高性能,所以在 Spring 中 Bean 的作用域也是 单例模式的
限定程序中变量的可用范围叫做作用域.
Bean的作用域是指, Bean 在 Spring 整个框架中的某种行为,比如 单例模式的作用域, 意思就是 Bean 在整个 Spring 中只有一份, 它是全局共享的, 当有人修改了这个值后, 其他人读到的就是被修改的值
Spring 容器在初始化一个 Bean 的实例时,会同时指定该实例的作用域,Spring中有 6 种作用域
- singleton: 单例模式 (默认,每次请求取的是同一个对象)
- prototype: 原型模式 (多例模式,每次请求创建一个新的对象)
- request: 请求作用域 (Spring MVC,每次请求创建一个对象)
- session: 会话作用域 (Spring MVC,在一个会话中共享一个对象)
- application: 全局作用域 (Spring MVC,所有人共用一个)
- websocket: HTTP WebSocket 作用域 (Spring WebSocket 项目中存在的一种)
使用 @Scope 标签来声明 Bean 的作用域,
@Scope 标签既可以修饰方法,也可以修饰类, @Scope 有两种设置方式
- 直接设置值: @Scope("prototype")
- 类似于枚举的常量方式: @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
- @Component
- public class Users {
- @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
- @Bean
- public User user1() {
- User user = new User();
- user.setId(1);
- user.setName("java");
- return user;
- }
- }
还是 案例1中的代码,只不过加上 Bean的作用域 原型模式后,运行程序,可以看到A修改后,B没有受到影响

Spring 执行流程:
- 启动 Spring 容器
- 实例化 Bean (分配内存空间)
- 将 Bean 注册到 Spring 中 (存操作)
- 将 Bean 装配到需要的类中 (取操作)

Bean 的生命周期:
- 实例化 Bean (给 Bean 分配内存空间) 买房子
- 设置属性 (Bean 对象注入) 装修
- Bean 初始化 买家电
- 执行各种通知 (执行各种 Aware)
- 执行初始化的前置方法
- 执行构造方法,两种执行方式,一种是执行 @PostConstruct,另一种是执行 init-method
- 执行初始化的后置方法
4. 使用 Bean 入住
5. 销毁 Bean 卖房
- @PreDestroy (销毁前方法)
- 重写 DisposableBean 接口方法
- destroy-method
注解 @PostConstruct
注解 @PreDestroy
- public class BeanLifeComponent implements BeanNameAware {
- @PostConstruct
- public void postConstruct() {
- System.out.println("执行 PostConstruct()");
- }
- public void init() {
- System.out.println("执行 init-method");
- }
- public void use() {
- System.out.println("使用 bean");
- }
- @PreDestroy
- public void preDestroy() {
- System.out.println("执行: preDestroy()");
- }
- public void setBeanName(String s) {
- System.out.println("执行了 Aware 通知");
- }
- }
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:content="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <content:component-scan base-package="com.beans">content:component-scan>
- <beans>
- <bean id="beanLifeComponent" class="com.beans.BeanLifeComponent" init-method="init">bean>
- beans>
- beans>
- public class BeanLifeTest {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config2.xml");
- BeanLifeComponent lifeComponent = context.getBean(BeanLifeComponent.class);
- lifeComponent.use();
- //执行销毁方法
- context.destroy();
- }
- }

先设置属性再进行初始化,作用是
如果先初始化,就会出现空引用了,所以了为了防止空引用,就要先设置属性注入,然后再进行初始化