<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [源碼系列:手寫spring] IOC第十三節:Bean作用域,增加prototype的支持

               為了幫助大家更深入的理解bean的作用域,特意將BeanDefinition的雙例支持留到本章節中,創建Bean,相關Reader讀取等邏輯都有所改動。

      內容介紹

              在Spring中,Bean的作用域(Scope)定義了Bean的生命周期和可見性。包括單例和原型,在本章節中我們將為Bean添加多例的支持,下面是Prototype作用域的幾個特征介紹:

      1. 多例(Prototype): Bean的prototype作用域表示每次從容器中獲取該Bean時都會創建一個新的實例。每個請求或依賴注入都會導致創建一個全新的Bean對象。

      2. 不受Spring容器管理: prototype作用域的Bean實例不受Spring容器的管理,容器在創建Bean之后不會再跟蹤它。這意味著Spring容器不會負責釋放或銷毀prototype作用域的Bean。一旦Bean被創建并交給調用者,調用者負責Bean的生命周期,包括銷毀。

      3. 適用場景: prototype作用域適用于那些每次獲取實例時都需要全新狀態的Bean,例如,HTTP請求的處理器或線程池中的任務。這樣可以確保每次獲取的Bean都是獨立的,不會影響其他實例。

              這里設計到設計模式中的原型模式,形象地說,可以將prototype作用域的Bean比喻為一個不斷復制的模具。每次你需要一個新的Bean實例時,Spring容器會根據模具再次制作一個全新的Bean。這個模具本身不受容器管理,而是由你自己管理它的生命周期。這與singleton作用域的Bean不同,后者是像一個單一的實例化對象,由容器管理其生命周期。
       

      當前bean生命周期 prototype作用域

              可見prototype類型的bean并不受spring容器的管理,你需要自己負責銷毀或釋放這些Bean實例,以防止內存泄漏。 

      代碼分支

      GitHub - yihuiaa/little-spring: 剖析Spring源碼,包括常見特性IOC、AOP、三級緩存等...剖析Spring源碼,包括常見特性IOC、AOP、三級緩存等... Contribute to yihuiaa/little-spring development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/yihuiaa/little-spring

      核心代碼

       BeanDefinition

          private String scope = SCOPE_SINGLETON;
          private boolean singleton = true;
          private boolean prototype = false;
          public static String SCOPE_SINGLETON = "singleton";
          public static String SCOPE_PROTOTYPE = "prototype";
      
          public void setScope(String scope) {
              this.scope = scope;
              this.singleton = SCOPE_SINGLETON.equals(scope);
              this.prototype = SCOPE_PROTOTYPE.equals(scope);
          }
      
          public boolean isSingleton() {
              return this.singleton;
          }
          public boolean isPrototype() {
              return this.prototype;
          }

      AbstractAutowireCapableBeanFactory#doCreateBean()

      ...	
      	protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {
      		Object bean = null;
      		try {
      			bean = createBeanInstance(beanDefinition);
      			//為bean填充屬性
      			applyPropertyValues(beanName, bean, beanDefinition);
      			//執行bean的初始化方法和BeanPostProcessor的前置和后置處理方法
      			initializeBean(beanName, bean, beanDefinition);
      		} catch (Exception e) {
      			throw new BeansException("Instantiation of bean failed", e);
      		}
      
      		//注冊有銷毀方法的bean
      		registerDisposableBeanIfNecessary(beanName, bean, beanDefinition);
      
      		if (beanDefinition.isSingleton()) {
      			addSingleton(beanName, bean);
      		}
      		return bean;
      	}
      ...

      AbstractAutowireCapableBeanFactory#registerDisposableBeanIfNecessary()

      	/**
      	 * 注冊有銷毀方法的bean,即bean繼承自DisposableBean或有自定義的銷毀方法
      	 *
      	 * @param beanName
      	 * @param bean
      	 * @param beanDefinition
      	 */
      	protected void registerDisposableBeanIfNecessary(String beanName, Object bean, BeanDefinition beanDefinition) {
      		//只有singleton類型bean會執行銷毀方法
      		if (beanDefinition.isSingleton()) {
      			if (bean instanceof DisposableBean || StrUtil.isNotEmpty(beanDefinition.getDestroyMethodName())) {
      				registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, beanDefinition));
      			}
      		}
      	}
      

      測試

      prototype-bean.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"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
      	         http://www.springframework.org/schema/beans/spring-beans.xsd
      		 http://www.springframework.org/schema/context
      		 http://www.springframework.org/schema/context/spring-context-4.0.xsd">
      
          <bean id="car" class="bean.Car" scope="prototype">
              <property name="name" value="Rolls-Royce"/>
          </bean>
      
      </beans>

      單元測試

      public class PrototypeBeanTest {
      	@Test
      	public void testPrototype() throws Exception {
      		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:prototype-bean.xml");
      
      		Car car1 = applicationContext.getBean("car", Car.class);
      		Car car2 = applicationContext.getBean("car", Car.class);
      		System.out.println(car1 == car2);;
      	}
      }
      

      測試結果

      false
      Process finished with exit code 0

       

      posted @ 2023-09-06 18:14  yihuiComeOn  閱讀(28)  評論(0)    收藏  舉報  來源
      主站蜘蛛池模板: 亚洲中文一区二区av| 宜川县| 一区二区三区四区五区自拍| 国产播放91色在线观看| 亚洲av永久无码天堂影院| 亚洲三区在线观看无套内射| 丝袜a∨在线一区二区三区不卡| 色欲天天婬色婬香综合网| 啪啪av一区二区三区| www成人国产高清内射| 国产无遮挡猛进猛出免费软件| 亚洲精品国产精品乱码不| 欧美牲交a欧美牲交aⅴ一| 在国产线视频A在线视频| av亚洲一区二区在线| 顶级少妇做爰视频在线观看| 亚洲人妻一区二区精品| 国内自拍小视频在线看| 最近免费中文字幕大全| 伊人精品成人久久综合97| 熟女在线视频一区二区三区| 少妇爽到呻吟的视频| 国产玖玖视频| 国产老妇伦国产熟女老妇高清| 极品少妇被猛得白浆直流草莓视频| 国产强奷在线播放免费| 无码人妻丝袜在线视频| 午夜福制92视频| 久久久久久av无码免费网站| 国产精品午夜福利视频| 一本一道久久综合狠狠老| 精品亚洲国产成人av| 无码人妻斩一区二区三区| 亚洲精品成人无限看| 欧美人与禽2o2o性论交| 午夜天堂av天堂久久久| 无套内谢少妇高清毛片| 国产高清色高清在线观看 | 鲁甸县| 日韩熟妇| 国产高颜值极品嫩模视频|