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

阿里云域名官网郑州网站seo推广

阿里云域名官网,郑州网站seo推广,网站建设方案范本,网站制作技术介绍转载自 Spring Bean 作用域 Bean 的作用域 当在 Spring 中定义一个 bean 时#xff0c;你必须声明该 bean 的作用域的选项。例如#xff0c;为了强制 Spring 在每次需要时都产生一个新的 bean 实例#xff0c;你应该声明 bean 的作用域的属性为 prototype。同理#xff…转载自  Spring Bean 作用域 Bean 的作用域 当在 Spring 中定义一个 bean 时你必须声明该 bean 的作用域的选项。例如为了强制 Spring 在每次需要时都产生一个新的 bean 实例你应该声明 bean 的作用域的属性为 prototype。同理如果你想让 Spring 在每次需要时都返回同一个bean实例你应该声明 bean 的作用域的属性为 singleton。 Spring 框架支持以下五个作用域如果你使用 web-aware ApplicationContext 时其中三个是可用的。 作用域描述singleton 在spring IoC容器仅存在一个Bean实例Bean以单例方式存在默认值 prototype每次从容器中调用Bean时都返回一个新的实例即每次调用getBean()时相当于执行newXxxBean()request每次HTTP请求都会创建一个新的Bean该作用域仅适用于WebApplicationContext环境session同一个HTTP Session共享一个Bean不同Session使用不同的Bean仅适用于WebApplicationContext环境global-session一般用于Portlet应用环境改作用于仅适用于WebApplicationContext环境 本章将讨论前两个范围当我们将讨论有关 web-aware Spring ApplicationContext 时其余三个将被讨论。 singleton 作用域 当一个bean的作用域为Singleton那么Spring IoC容器中只会存在一个共享的bean实例并且所有对bean的请求只要id与该bean定义相匹配则只会返回bean的同一实例。 Singleton是单例类型就是在创建起容器时就同时自动创建了一个bean的对象不管你是否使用他都存在了每次获取到的对象都是同一个对象。注意Singleton作用域是Spring中的缺省作用域。你可以在 bean 的配置文件中设置作用域的属性为 singleton如下所示 !-- A bean definition with singleton scope -- bean id... class... scopesingleton!-- collaborators and configuration for this bean go here -- /bean 例子 我们在适当的位置使用 Eclipse IDE然后按照下面的步骤来创建一个 Spring 应用程序 步骤描述1创建一个名称为 SpringExample 的项目并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint。2使用 Add External JARs 选项添加所需的 Spring 库在 Spring Hello World Example 章节解释。3在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp。4在 src 文件夹中创建 Beans 配置文件 Beans.xml。5最后一步是创建的所有 Java 文件和 Bean 配置文件的内容并运行应用程序解释如下。 这里是 HelloWorld.java 文件的内容 package com.tutorialspoint; public class HelloWorld {private String message;public void setMessage(String message){this.message message;}public void getMessage(){System.out.println(Your Message : message);} } 下面是 MainApp.java 文件的内容 package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(Beans.xml);HelloWorld objA (HelloWorld) context.getBean(helloWorld);objA.setMessage(Im object A);objA.getMessage();HelloWorld objB (HelloWorld) context.getBean(helloWorld);objB.getMessage();} } 下面是 singleton 作用域必需的配置文件 Beans.xml ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdbean idhelloWorld classcom.tutorialspoint.HelloWorld scopesingleton/bean/beans 一旦你创建源代码和 bean 配置文件完成后我们就可以运行该应用程序。如果你的应用程序一切都正常将输出以下信息 Your Message : Im object A Your Message : Im object A prototype 作用域 当一个bean的作用域为Prototype表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求将其注入到另一个bean中或者以程序的方式调用容器的getBean()方法时都会创建一个新的bean实例。Prototype是原型类型它在我们创建容器的时候并没有实例化而是当我们获取bean的时候才会去创建一个对象而且我们每次获取到的对象都不是同一个对象。根据经验对有状态的bean应该使用prototype作用域而对无状态的bean则应该使用singleton作用域。 为了定义 prototype 作用域你可以在 bean 的配置文件中设置作用域的属性为 prototype如下所示 !-- A bean definition with singleton scope -- bean id... class... scopeprototype!-- collaborators and configuration for this bean go here -- /bean 例子 我们在适当的位置使用 Eclipse IDE然后按照下面的步骤来创建一个 Spring 应用程序 步骤描述1创建一个名称为 SpringExample 的项目并且在创建项目的 src 文件夹中创建一个包com.tutorialspoint。2使用 Add External JARs 选项添加所需的 Spring 库解释见 Spring Hello World Example 章节。3在 com.tutorialspoint 包中创建 Java 类 HelloWorld 和 MainApp。4在 src 文件夹中创建 Beans 配置文件Beans.xml。5最后一步是创建的所有 Java 文件和 Bean 配置文件的内容并运行应用程序解释如下所示。 这里是 HelloWorld.java 文件的内容 package com.tutorialspoint;public class HelloWorld {private String message;public void setMessage(String message){this.message message;}public void getMessage(){System.out.println(Your Message : message);} } 下面是 MainApp.java 文件的内容 package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(Beans.xml);HelloWorld objA (HelloWorld) context.getBean(helloWorld);objA.setMessage(Im object A);objA.getMessage();HelloWorld objB (HelloWorld) context.getBean(helloWorld);objB.getMessage();} } 下面是 prototype 作用域必需的配置文件 Beans.xml ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdbean idhelloWorld classcom.tutorialspoint.HelloWorld scopeprototype/bean/beans 一旦你创建源代码和 Bean 配置文件完成后我们就可以运行该应用程序。如果你的应用程序一切都正常将输出以下信息 Your Message : Im object A Your Message : null
http://www.yutouwan.com/news/155986/

相关文章:

  • 百度 网站地图怎么做重庆市建设厅官方网站
  • 公司网站建设需要什么wordpress 商务 主题
  • 磐石网站seo东莞网站推广营销网站设计
  • 中国网站建设哪家公司好衡水企业网站制作报价
  • 多仓库版仓库管理网站建设源码wordpress 首页文章数量
  • 织梦示范网站步骤的近义词
  • 怎么做网站版面苏州吴中区做网站公司
  • 大连品尚茗居装修公司怎么样企业网站seo平台
  • 做挂网站吗东莞网络优化推广
  • 济南百度做网站怎么经营团购网站
  • 网站建设产品话术陕西省住房和城乡建设厅综合网站
  • 浏览器打不开wordpress百度seo和sem的区别
  • 怎么样模仿一个网站做简历网站建设应该注意什么
  • wordpress 仿站焦作网站设计
  • 工信部企业网站备案吗深圳网站建设方案优化
  • 西乡网站的建设自建本地网站服务器wordpress
  • 国外工装设计网站大全做全景网站
  • 一个好的网站应该具有什么条件wordpress 提交审批
  • 广西建设工程造价管理协会网站网站开发入那个科目
  • nat123做视频网站天津 app开发公司
  • 网站建设常识公司注册要多少费用
  • 扁平化设计风格的网站模板免费下载建设公司网站需要多少天
  • 网站的备案号汕头seo服务
  • 一级做爰片c视频网站无锡网站设计哪家公司好
  • 邢台做网站优化用家用路由器ip做网站
  • 网站建设如何把更改内容wordpress显示注册ip
  • 如何评价一个网站做的是否好网站开发和网站建设
  • 做质量计量的网站有哪些电脑公司网站设计
  • 遵义网站建设服务wordpress调取某页面
  • 免费的招聘网站有哪些广州seo学徒