1. 程式人生 > >Spring短生命週期bean注入長生命週期bean問題

Spring短生命週期bean注入長生命週期bean問題

Spring中Bean的生命週期有singleton prototype request session global session application,預設的生命週期是singleton在容器中永遠都只有一份例項,prototype是原型模式即每一次呼叫都生產一個新的Bean例項。requestsessionglobal sessionapplication是和web應用相關的。

public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {

	/**
	 * Scope identifier for the standard singleton scope: "singleton".
	 * Custom scopes can be added via {@code registerScope}.
	 * @see #registerScope
	 */
	String SCOPE_SINGLETON = "singleton";

	/**
	 * Scope identifier for the standard prototype scope: "prototype".
	 * Custom scopes can be added via {@code registerScope}.
	 * @see #registerScope
	 */
	String SCOPE_PROTOTYPE = "prototype";

public interface WebApplicationContext extends ApplicationContext {

	/**
	 * Context attribute to bind root WebApplicationContext to on successful startup.
	 * <p>Note: If the startup of the root context fails, this attribute can contain
	 * an exception or error as value. Use WebApplicationContextUtils for convenient
	 * lookup of the root WebApplicationContext.
	 * @see org.springframework.web.context.support.WebApplicationContextUtils#getWebApplicationContext
	 * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
	 */
	String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";

	/**
	 * Scope identifier for request scope: "request".
	 * Supported in addition to the standard scopes "singleton" and "prototype".
	 */
	String SCOPE_REQUEST = "request";

	/**
	 * Scope identifier for session scope: "session".
	 * Supported in addition to the standard scopes "singleton" and "prototype".
	 */
	String SCOPE_SESSION = "session";

	/**
	 * Scope identifier for global session scope: "globalSession".
	 * Supported in addition to the standard scopes "singleton" and "prototype".
	 */
	String SCOPE_GLOBAL_SESSION = "globalSession";

	/**
	 * Scope identifier for the global web application scope: "application".
	 * Supported in addition to the standard scopes "singleton" and "prototype".
	 */
	String SCOPE_APPLICATION = "application";

       多例注入給單例,1註解的方式;2xml檔案的方式;3依靠查詢的方式.1,2是通過動態代理的方式實現的。3是依靠每次使用Scope物件是多通過查詢的方式來獲取對應生命週期的物件。

相關推薦

Spring生命週期bean注入生命週期bean問題

Spring中Bean的生命週期有singleton prototype request session global session application,預設的生命週期是singleton在容器

Spring——使用cglib更新生命週期中引用的生命週期bean

在長生命週期的bean引用短生命週期bean時,會有一個問題。 比如singleton 類依賴了prototype類,容器會在singleton 類初始化時,就根據依賴關係將prototype類注入。以後的每一次呼叫singleton bean都是同一個物件,裡

Spring IOC -bean物件的生命週期詳解(補充,含程式碼)

生命週期執行的過程如下:1) spring對bean進行例項化,預設bean是單例2) spring對bean進行依賴注入3) 如果bean實現了BeanNameAware介面,spring將bean的id傳給setBeanName()方法4) 如果bean實現了BeanFactory

Spring IOC -bean物件的生命週期詳解

生命週期執行的過程如下:1)spring對bean進行例項化,預設bean是單例2)spring對bean進行依賴注入3)如果bean實現了BeanNameAware介面,spring將bean的id傳給setBeanName()方法4)如果bean實現了BeanFactoryAware介面,spring將呼

一張圖搞懂Spring bean的完整生命週期

一張圖搞懂Spring bean的生命週期,從Spring容器啟動到容器銷燬bean的全過程,包括下面一系列的流程,瞭解這些流程對我們想在其中任何一個環節怎麼操作bean的生成及修飾是非常有幫助的。 Bean的完整生命

SpringBean的LifeCycle(生命週期)

菜瓜:水稻,上次說Bean的LifeCycle,還沒講完 水稻:啥?說人話? 菜瓜:spring,bean,生命週期 水稻:哦哦,下次直接說人話。說正事,先從BeanFactory、ApplicationContext和FactoryBean開始說起吧 我們知道(你不一定知道)BeanFactory是Spr

Spring Bean各階段生命週期的介紹

一.xml方式配置bean 二.Aware介面   2.1 BeanNameAware   2.2 BeanFactoryAware   2.3 ApplicationContextAware   2.4 Aware各介面的執行順序   2.4 Aware介面總結 三.

Spring中與bean有關的生命週期

### 前言 記得以前的時候,每次提起Spring中的bean相關的生命週期時,內心都無比的恐懼,因為好像有很多,自己又理不清楚:什麼beanFactory啊,aware介面啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。 今天終於理清這些關係了

Bean管理之生命週期之BeanPostProcessor

package com.imooc.ioc.demo3; public interface UserDao { public void findAll(); public void

spring Bean的完整生命周期

destroy pri 重要 processor 上下文對象 nts tro oid sso spring 容器中的bean的完整生命周期一共分為十一步完成。 1.bean對象的實例化 2.封裝屬性,也就是設置properties中的屬性值 3.如果bean實現了Be

.Net核心依賴項注入生命週期和最佳實踐

![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20201117221722916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG

2021年的UWP(6)——生命週期Desktop Extension向UWP的反向通知

上一篇我們討論了UWP和Desktop Extension間的雙向通訊,適用於Desktop Extension中存在使用者互動的場景。本篇我們討論最後一種情況,與前者不同的是,Desktop Extension和UWP保持相同的生命週期,同時規避AppServiceConnection可能被Windows回

Spring Bean注入/單例理解/迴圈依賴

理解迴圈依賴問題,首先明白spring有四種注入方式。 第一種,SET注入a類中持有b類的引用,並且a類有b的set方法。在bean中新增<property>標籤即可注入。實質上是將b例項化,然後呼叫set方法注入。 <bean id="a" class="com.qunar.poj

普通Java類獲取spring 容器的bean的5種方法 Spring注入非單例bean以及scope的作用範圍

本文轉載自:http://www.cnblogs.com/duanxz/archive/2014/06/18/3794075.html 方法一:在初始化時儲存ApplicationContext物件方法二:通過Spring提供的工具類獲取ApplicationContext物件方法三:繼承自抽象類Appli

[轉載] Linux export變數的生命週期和shell的生命週期相同,即shell指令碼執行完畢後,相應的export變數便失效了

說說shell指令碼中的export 和 source,bash 小弟剛剛接觸linux,對linux上的很多東西都比較陌生,所以寫一寫部落格,當做自己工作的總結和技術的積累吧,也是鞭策自己不斷努力的去學習。 今天之所以起這個標題,把export,source ,bash這三個命令放在一起講

Spring注入bean報錯 Error creating bean with name的網上找不到的解決方案

                        今天在研究Bas

Spring 多執行緒下注入bean問題詳解

本文介紹了Spring 多執行緒下注入bean問題詳解,分享給大家,具體如下: 問題 Spring中多執行緒注入userThreadService注不進去,顯示userThreadService為null異常 程式碼如下: public class UserThreadTask implements

Spring通過註解annotation方式注入Bean時,採用動態代理,那麼JDK代理和CGLIB代理區別?

切面程式設計是Spring中非常重要的一個模組,切面程式設計的實現原理是動態代理,那麼動態代理又有兩種實現方式:一種方法是直接實現JDK中的InvocationHandler介面,另一種方法是繼承CGLIB。 首先如果不是很清楚兩者的區別的話,記住一般情況下Invocati

springbean配置和bean注入

轉自 https://www.cnblogs.com/wuchanming/p/5426746.html bean與spring容器的關係 Bean配置資訊定義了Bean的實現及依賴關係,Spring容器根據各種形式的Bean配置資訊在容器內部建立Bean定義

Android開發— Activity生命週期和fragment的生命週期

(圖片就借鑑一下其他作者的,見諒!) Activity的生命週期在業務邏輯上的處理一定要慎重!!! Fragment的生命週期同樣十分重要,並且要常用frgment懶載入方案更要注意這種方式,其生命週期圖 同時我們都知道 Fragment 是依賴於 Acti