<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [toc] bean在定義的時候可以指定范圍,spring提供了6種范圍,其中4種是web應用環境相關的. | 范圍 | 說明 | | --- | --- | |singleton | 默認的,一個bean定義,只有一個對象實例 | | prototype | 一個bean定義,可以有任意對象實例 | | request | web環境下,一個bean定義,每次http請求生命周期,對應一個對象實例 | | session | web環境下,一個bean定義,每次http會話生命周期,對應一個對象實例 | | application | web環境下,一個bean定義,每次servlet上下文生命周期,對應一個對象實例 | |websocket |web環境下,一個bean定義,每次websocket請求生命周期,對應一個對象實例 | ## 1.5.1. The singleton scope 單例范圍 ![singletone](https://box.kancloud.cn/a1e5415e8969df3b1fa9ab3cf9bfe7b7_800x398.png) spring的單例不同于(Gang of Four (GoF) patterns book)設計模式的單例,設計模式的單例是通過編碼控制一個`ClassLoader`只創建一個類的對象實例.spring的單例則是一個容器一個bean. xml定義如下 ~~~ <bean id="accountService" class="com.foo.DefaultAccountService"/> <!-- the following is equivalent, though redundant (singleton scope is the default) --> <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/> ~~~ ## 1.5.2. The prototype scope 原型范圍,每次注入其他bean或者調用`getBean()`方法,獲取的都是新的對象實例 一般的規則是,有狀態的bean使用原型范圍,無狀態的bean使用單例范圍 ![prototype](https://box.kancloud.cn/73bdfe9ece22e1ae54cfe4282664f27f_800x397.png) xml定義如下: ~~~ <bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/> ~~~ ## 1.5.3. Singleton beans with prototype-bean dependencies 如果在單例范圍的bean中注入原型范圍的bean,考慮注入是發生在實例化過程中的,因此,在實例化單例范圍的bean時,也只有一次注入的原型范圍的bean機會,所以,單例bean中的原型,其實是同一個對象, 如果想在單例bean中,每次使用的原型bean都是新的,則參考[method injection](https://docs.spring.io/spring/docs/5.0.7.RELEASE/spring-framework-reference/core.html#beans-factory-method-injection) ## 1.5.4. Request, session, application, and WebSocket scopes `request, session, application`, and` websocket`這幾個范圍只能在web環境中使用,例如`XmlWebApplicationContext`,如果使用其他非web環境的上下文,如`ClassPathXmlApplicationContext`,則會發生異常`IllegalStateException` ### Initial web configuration 為了能正常使用web環境下的幾個范圍,需要做一些初始化(單例和原型的并不需要),不同的環境有不同的初始化方式 如果是通過spring web mvc訪問bean,請求會經過spring內部的`DispatcherServlet`處理,不再需要其他設置,`DispatcherServlet`已經暴露了所有的相關狀態. 如果使用servlet2.5 web容器,請求會經過spring 外部的`DispatcherServlet`(例如JSF,Struts),需要注冊`org.springframework.web.context.request.RequestContextListener ServletRequestListener`,對于servlet3.0,可以編碼實現`WebApplicationInitializer `接口.其他,或更老的容器,添加下面的聲明到`web.xm`l文件 ~~~ <web-app> ... <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> ... </web-app> ~~~ TODO
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看