网站开发的背景知识与相关技术,域名免费,乐清新闻综合频道,wordpress 返回1.通过⼀个案例来看 Bean 作⽤域的问题
假设现在有⼀个公共的 Bean#xff0c;提供给 A ⽤户和 B ⽤户使⽤#xff0c;然⽽在使⽤的途中 A ⽤户却“悄悄”地修 改了公共 Bean 的数据#xff0c;导致 B ⽤户在使⽤时发⽣了预期之外的逻辑错误。
1.1 被修改的 Bean 案例
公…1.通过⼀个案例来看 Bean 作⽤域的问题
假设现在有⼀个公共的 Bean提供给 A ⽤户和 B ⽤户使⽤然⽽在使⽤的途中 A ⽤户却“悄悄”地修 改了公共 Bean 的数据导致 B ⽤户在使⽤时发⽣了预期之外的逻辑错误。
1.1 被修改的 Bean 案例
公共的bean
Beanpublic User user2(){User user new User();user.setName(lisi);user.setAge(19);return user;}
A ⽤户使⽤时进⾏了修改操作
Controller
public class ScopeController {Autowiredprivate User user2;public void sayHi(){System.out.println(user2);user2.setName(张三获取的user2);System.out.println(user2);}
}B ⽤户再去使⽤公共 Bean 的时候
Controller
public class ScopeController2 {Autowiredprivate User user2;public void sayHi(){System.out.println(user2);}
}
打印 A ⽤户和 B ⽤户公共 Bean 的值
public class App {public static void main(String[] args) {// 1.得到 spring 上下⽂ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml);
ScopeController scopeController context.getBean(ScopeController.class);scopeController.sayHi();ScopeController2 scopeController2 context.getBean(ScopeController2.class);scopeController2.sayHi();}}
执⾏结果如下 1.2 原因分析
操作以上问题的原因是因为 Bean 默认情况下是单例状态singleton也就是所有⼈的使⽤的都是同 ⼀个对象之前我们学单例模式的时候都知道使⽤单例可以很⼤程度上提⾼性能所以在 Spring 中 Bean 的作⽤域默认也是 singleton 单例模式。
2.作用域定义
限定程序中变量的可⽤范围叫做作⽤域或者说在源代码中定义变量的某个区域就叫做作⽤域。 ⽽ Bean 的作⽤域是指 Bean 在 Spring 整个框架中的某种⾏为模式⽐如 singleton 单例作⽤域就 表示 Bean 在整个 Spring 中只有⼀份它是全局共享的那么当其他⼈修改了这个值之后那么另⼀ 个⼈读取到的就是被修改的值。
2.1 Bean 的 6 种作⽤域
Spring 容器在初始化⼀个 Bean 的实例时同时会指定该实例的作⽤域。Spring有 6 种作⽤域最后 四种是基于 Spring MVC ⽣效的
1. singleton单例作⽤域
2. prototype原型作⽤域多例作⽤域
3. request请求作⽤域
4. session回话作⽤域
5. application全局作⽤域
6. websocketHTTP WebSocket 作⽤域
注意后 4 种状态是 Spring MVC 中的值在普通的 Spring 项⽬中只有前两种。
spring官方文档 singleton
官方说明(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.描述该作⽤域下的Bean在IoC容器中只存在⼀个实例获取Bean即通过 applicationContext.getBean等⽅法获取及装配Bean即通过Autowired注⼊都是同⼀个对 象。场景通常⽆状态的Bean使⽤该作⽤域。⽆状态表示Bean对象的属性状态不需要更新备注Spring默认选择该作⽤域
prototype
官⽅说明Scopes a single bean definition to any number of object instances.描述每次对该作⽤域下的Bean的请求都会创建新的实例获取Bean即通过 applicationContext.getBean等⽅法获取及装配Bean即通过Autowired注⼊都是新的对象 实例。场景通常有状态的Bean使⽤该作⽤域
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中使⽤
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中使⽤
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中使⽤
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中使⽤
单例作⽤域(singleton) VS 全局作⽤域(application)
singleton 是 Spring Core 的作⽤域application 是 Spring Web 中的作⽤域singleton 作⽤于 IoC 的容器⽽ application 作⽤于 Servlet 容器。
2.2 设置作⽤域
使⽤ Scope 标签就可以⽤来声明 Bean 的作⽤域⽐如设置 Bean 的作⽤域如下代码所示
Configuration
public class BeanConfig {Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)Beanpublic User user2(){User user new User();user.setName(lisi);user.setAge(19);return user;}
} Scope 标签既可以修饰⽅法也可以修饰类Scope 有两种设置⽅式
1. 直接设置值Scope(prototype)
2. 使⽤枚举设置Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
3. Spring 执行流程和 Bean 的生命周期
3.1 Spring 执行流程 Bean 执⾏流程Spring 执⾏流程启动 Spring 容器 - 实例化 Bean分配内存空间从⽆到 有 - Bean 注册到 Spring 中存操作 - 将 Bean 装配到需要的类中取操作)。
3.2 Bean ⽣命周期
所谓的⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程我们把这个过程就叫做⼀个对象的⽣命 周期。 Bean 的⽣命周期分为以下 5 ⼤部分
1.实例化 Bean为 Bean 分配内存空间
2.设置属性Bean 注⼊和装配
3.Bean 初始化
实现了各种 Aware 通知的⽅法如 BeanNameAware、BeanFactoryAware、 ApplicationContextAware 的接⼝⽅法执⾏ BeanPostProcessor 初始化前置⽅法执⾏ PostConstruct 初始化⽅法依赖注⼊操作之后被执⾏执⾏⾃⼰指定的 init-method ⽅法如果有指定的话执⾏ BeanPostProcessor 初始化后置⽅法。
4.使⽤ Bean
5.销毁 Bean 销毁容器的各种⽅法如 PreDestroy、DisposableBean 接⼝⽅法、destroy-method。 执⾏流程如下图所示 实例化和初始化的区别
实例化和属性设置是 Java 级别的系统“事件”其操作过程不可⼈⼯⼲预和修改⽽初始化是给开发者 提供的可以在实例化之后类加载完成之前进⾏⾃定义“事件”处理。
生命流程的“故事”
Bean 的⽣命流程看似繁琐但咱们可以以⽣活中的场景来理解它⽐如我们现在需要买⼀栋房⼦那 么我们的流程是这样的
1. 先买房实例化从⽆到有
2. 装修设置属性
3. 买家电如洗⾐机、冰箱、电视、空调等[各种]初始化
4. ⼊住使⽤ Bean
5. 卖出去Bean 销毁。
生命周期演示
package org.example.component;import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/*** bean的声明周期*/
//Component
public class BeanLifeComponent implements BeanNameAware {public BeanLifeComponent(){System.out.println(执行了构造函数。。。);}Overridepublic void setBeanName(String s){System.out.println(设置beanname:s);}PostConstructpublic void postConstruct(){System.out.println(执行postConstruct方法);}public void init(){System.out.println(执行init方法);}public void hi(){System.out.println(hi~);}PreDestroypublic void destroy(){System.out.println(执行destroy方法。。。);}
}xml 配置如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contenthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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.xsdcontent:component-scan base-packageorg.example.component/content:component-scanbean idbeanlife classorg.example.component.BeanLifeComponent init-methodinit/bean
/beans 调⽤类
import javafx.application.Application;
import org.example.component.BeanLifeComponent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml);BeanLifeComponent beanLifeComponent context.getBean(BeanLifeComponent.class);beanLifeComponent.hi();context.destroy();}
}4.总结
本篇文章介绍了 Bean 的 6 种作⽤域
1. singleton单例作⽤域
2. prototype原型作⽤域多例作⽤域
3. request请求作⽤域
4. session回话作⽤域
5. application全局作⽤域 6
. websocketHTTP WebSocket 作⽤域
其中前两种是 spring 核⼼作⽤域⽽后 4 种是 spring mvc 中的作⽤域
也介绍了 spring 的执⾏流程 和 bean 的⽣命周期其中 bean 的作⽤域是最重要的知识点也是常⻅的⾯试题⽽ bean ⼤的执⾏流 程也⼀定要牢记。