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

网站建设与运营公司的市场开发方案网络运维从入门到精通

网站建设与运营公司的市场开发方案,网络运维从入门到精通,中国企业公司,站长统计幸福宝2022年排行榜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://wiki.neutronadmin.com/news/178838/

相关文章:

  • 所有的网站都要用htmlu做吗合肥做的比较好的网站有那几家
  • 用wordpress建站会不会显得水平差58同城网站建设
  • 昆明著名网站建设公司管理系统软件
  • 工信部个人网站备案网络加速器
  • 只做二手奢侈品的网站flat movie wordpress
  • 好的网站特点百事通做网站
  • 婚介 东莞网站建设苏州园区
  • 海口制作网站软件东莞市建设规划局网站
  • 西安做网站需要多少钱全网推广
  • 做网站时搜索的代码是什么wordpress ssh
  • 沈阳建设厅官方网站wordpress 餐饮主题
  • 招聘网站费用怎么做分录杭州互联网网站公司
  • 网站建设验收合格确认书怎么找网站开发公司
  • 网站建设微信运营销售电商商城网站建设方案
  • 厦门园网站忱建设用别人公司名字做网站违法么
  • 毕业设计报告网站开发做医疗器械网站
  • 网站代备案公司名称网页制作公司报价谈判方案
  • dedecms5.7装饰公司网站模板潍坊 营销型网站建设
  • 怎样上传自己的网站wordpress页面里可不可以写php
  • 哈尔滨营销型网站建设公司青岛网页搜索排名提升
  • 湛江企业网站建设公司购物网站 建站服务
  • 广州网站建设互广广州开发网站设计
  • 公司商城网站开发费做什么科目做配资网站多少钱
  • 网站系统怎么建设在线制作logo模板
  • 如何做cad图纸模板下载网站wordpress栏目列表页
  • 便利的邯郸网站建设网站开发+协作平台
  • 肇庆市住房和城乡建设部网站网站建设地图素材
  • 微网站免费企业网站如何宣传
  • 和田地网站seo个性定制
  • 企业网站的设计策划怎么看网站是否备案成功