多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

wkspring教程

Spring Bean 作用域

閱讀 (2170)

Bean 的作用域

當(dāng)在 Spring 中定義一個(gè) 時(shí),你必須聲明該 bean 的作用域的選項(xiàng)。例如,為了強(qiáng)制 Spring 在每次需要時(shí)都產(chǎn)生一個(gè)新的 bean 實(shí)例,你應(yīng)該聲明 bean 的作用域的屬性為 prototype。同理,如果你想讓 Spring 在每次需要時(shí)都返回同一個(gè)bean實(shí)例,你應(yīng)該聲明 bean 的作用域的屬性為 singleton

Spring 框架支持以下五個(gè)作用域,如果你使用 web-aware ApplicationContext 時(shí),其中三個(gè)是可用的。

作用域描述
singleton該作用域?qū)?bean 的定義的限制在每一個(gè) Spring IoC 容器中的一個(gè)單一實(shí)例(默認(rèn))。
prototype該作用域?qū)我?bean 的定義限制在任意數(shù)量的對(duì)象實(shí)例。
request該作用域?qū)?bean 的定義限制為 HTTP 請(qǐng)求。只在 web-aware Spring ApplicationContext 的上下文中有效。
session該作用域?qū)?bean 的定義限制為 HTTP 會(huì)話。 只在web-aware Spring ApplicationContext的上下文中有效。
global-session該作用域?qū)?bean 的定義限制為全局 HTTP 會(huì)話。只在 web-aware Spring ApplicationContext 的上下文中有效。

本章將討論前兩個(gè)范圍,當(dāng)我們將討論有關(guān) web-aware Spring ApplicationContext 時(shí),其余三個(gè)將被討論。

singleton 作用域:

如果作用域設(shè)置為 singleton,那么 Spring IoC 容器剛好創(chuàng)建一個(gè)由該 bean 定義的對(duì)象的實(shí)例。該單一實(shí)例將存儲(chǔ)在這種單例 bean 的高速緩存中,以及針對(duì)該 bean 的所有后續(xù)的請(qǐng)求和引用都返回緩存對(duì)象。

默認(rèn)作用域是始終是 singleton,但是當(dāng)僅僅需要 bean 的一個(gè)實(shí)例時(shí),你可以在 bean 的配置文件中設(shè)置作用域的屬性為 singleton,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <!-- collaborators and configuration for this bean go here -->
</bean>

例子

我們?cè)谶m當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟描述
1創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint
2使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫,在 Spring Hello World Example 章節(jié)解釋。
3com.tutorialspoint 包中創(chuàng)建 Java 類 HelloWorldMainApp
4src 文件夾中創(chuàng)建 Beans 配置文件 Beans.xml
5最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運(yùn)行應(yīng)用程序,解釋如下。

這里是 HelloWorld.java 文件的內(nèi)容:

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 文件的內(nèi)容:

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("I'm object A");
      objA.getMessage();
      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
      objB.getMessage();
   }
}

下面是 singleton 作用域必需的配置文件 Beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" 
      scope="singleton">
   </bean>

</beans>

一旦你創(chuàng)建源代碼和 bean 配置文件完成后,我們就可以運(yùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下信息:

Your Message : I'm object A
Your Message : I'm object A

prototype 作用域

如果作用域設(shè)置為 prototype,那么每次特定的 bean 發(fā)出請(qǐng)求時(shí) Spring IoC 容器就創(chuàng)建對(duì)象的新的 Bean 實(shí)例。一般說來,滿狀態(tài)的 bean 使用 prototype 作用域和沒有狀態(tài)的 bean 使用 singleton 作用域。

為了定義 prototype 作用域,你可以在 bean 的配置文件中設(shè)置作用域的屬性為 prototype,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype">
   <!-- collaborators and configuration for this bean go here -->
</bean>

例子

我們?cè)谶m當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟描述
1創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包com.tutorialspoint
2使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。
3com.tutorialspoint 包中創(chuàng)建 Java 類 HelloWorldMainApp
4src 文件夾中創(chuàng)建 Beans 配置文件Beans.xml
5最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運(yùn)行應(yīng)用程序,解釋如下所示。

這里是 HelloWorld.java 文件的內(nèi)容:

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 文件的內(nèi)容:

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("I'm object A");
      objA.getMessage();
      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
      objB.getMessage();
   }
}

下面是 prototype 作用域必需的配置文件 Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" 
      scope="prototype">
   </bean>

</beans>

一旦你創(chuàng)建源代碼和 Bean 配置文件完成后,我們就可以運(yùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下信息:

Your Message : I'm object A
Your Message : null
關(guān)閉
程序員人生
主站蜘蛛池模板: 天天看毛片 | 日本精品一区二区三区在线观看 | 国产亚洲一区二区精品 | 欧美在线性 | 日韩精品一区二区三区在线观看l | 欧美日韩一区二区亚洲 | 野外xxxxfreexxxx自己| 欧美在线aa | 国产日韩高清一区二区三区 | 国产精品自在线拍国产 | 日韩免费一区 | 精品国产日韩亚洲一区二区 | 一级毛片不卡片免费观看 | 成人爱av18丰满 | 一本到亚洲网 | 最近最新中文字幕免费大全 | 男女69视频 | 一级做a爰片性色毛片男 | 国产69精品久久久久妇女 | 久久久久亚洲精品影视 | 亚洲图片校园另激情类小说 | 久久99国产精品成人 | 欧美成人免费网在线观看 | 稀缺资源呦视频在线网站 | 国产极品美女在线 | 最近更新中文字幕7 | 久久精品国产欧美 | 久久久久国产视频 | 欧美精品三区 | 日本成人在线免费 | freee性欧美 freefromevideos性欧美 | 亚洲欧美综合国产精品一区 | 亚洲第一区二区快射影院 | 国产精品麻豆高清在线观看 | 免费国产成高清人在线视频 | 综合色在线观看 | 久久久久久岛国免费网站 | 国产精品嫩草影院免费看 | 亚洲第一中文字幕 | poronovideos巴西极品 | 免费观看又污又黄网站日本 |