当前位置: 首页 > news >正文

网站开发的背景知识与相关技术域名免费

网站开发的背景知识与相关技术,域名免费,乐清新闻综合频道,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 ⼤的执⾏流 程也⼀定要牢记。
http://www.pierceye.com/news/100032/

相关文章:

  • 网站和平台有什么区别黄页88怎么发信息质量高
  • 阿里建站价格小户型室内装修设计公司网站
  • 建设银行网站安全性分析网络推广服务平台
  • 大型购物网站建设福建微网站建设公司
  • 做网站软件j程序员找工作网站
  • 济南网站建设系统画册设计公司宣传册
  • 上海网站设计方案家纺网站建设
  • 衡水精品网站建设游戏广告推广平台
  • 响应式企业网站建设营销战略
  • wordpress离线浏览搜索引擎优化包括
  • 门户网站建设需要多少呼伦贝尔市住房和城乡建设局网站
  • 静海集团网站建设住房城乡建设网站
  • 个人备案挂企业网站网站开发公司照片
  • 网站建设课程体会国内最新新闻简短
  • 网站开发大概价格最常用的网页制作软件
  • 商务网站模块设计时前台基础设施建设免费网站建设空间
  • 青海省公路工程建设总公司网站饮料公司网站模板
  • 建设部网站刘赵云网页版邮箱
  • 免费扑克网站企业网站怎么搜索优化
  • 做网站导航的厦门网站建设制作多少钱
  • 怎样免费注册网站域名鹤城建设集团网站
  • 3合1网站建设价格网站建设论坛快速建站
  • 怎样做钓鱼网站上海网站关键词排名优化报价
  • 昆明专业网站设计公司电商类网站设计模板
  • 网站流量用完了重庆网站推广
  • 网站管理助手数据库网站在建设中无法访问
  • 网站标题格式建设网站南昌
  • wordpress作企业网站好吗沈阳短视频制作公司
  • 表格网站怎么做的作文网站大全
  • 比特币网站建设专业网站建设企业网站制作