<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 功能強大 支持多語言、二開方便! 廣告
                # Spring – Bean 生命周期 > 原文: [https://howtodoinjava.com/spring-core/spring-bean-life-cycle/](https://howtodoinjava.com/spring-core/spring-bean-life-cycle/) 在本文中,了解**SpringBean 生命周期**。 我們將學習生命周期階段,初始化和銷毀??回調方法。 我們將學習使用 XML 配置以及注解配置來控制 bean 生命周期事件。 ## 1\. Bean 的生命周期 當容器啟動時 – 需要基于 Java 或 XML bean 定義實例化 Spring bean。 可能還需要執行一些初始化后的步驟,以使其進入可用狀態。 相同的 bean 生命周期也適用于 [SpringBoot](https://howtodoinjava.com/spring-boot-tutorials/) 應用程序。 之后,當不再需要該 bean 時,它將被從 IoC 容器中刪除。 Spring bean 工廠負責管理通過 spring 容器創建的 bean 的生命周期。 #### 1.1. 生命周期回調 Spring bean 工廠控制 bean 的創建和銷毀。 為了執行一些自定義代碼,它提供了回調方法,這些方法可以大致分為兩類: * **初始化后**回調方法 * **銷毀**回調方法 #### 1.1. 生命周期圖示 ![Spring Bean Life Cycle](https://img.kancloud.cn/77/1b/771bf7b19fb901a497aea74eb7bb044e_524x408.jpg) Spring Bean Life Cycle ## 2\. 生命周期回調方法 Spring 框架提供了以下 **4 種方式來控制 Bean 的生命周期事件**: 1. `InitializingBean`和`DisposableBean`回調接口 2. *了解特定行為的界面 3. bean 配置文件中的自定義`init()`和`destroy()`方法 4. `@PostConstruct`和`@PreDestroy`注解 #### 2.1. `InitializingBean`和`DisposableBean` [`org.springframework.beans.factory.InitializingBean`](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/InitializingBean.html) 接口允許 Bean 在容器上設置了所有必需的屬性后執行初始化工作。 `InitializingBean`接口指定一種方法: `InitializingBean.java` ```java void afterPropertiesSet() throws Exception; ``` 這不是初始化 bean 的首選方法,因為它將 bean 類與 spring 容器緊密耦合。 更好的方法是在`applicationContext.xml`文件中的 bean 定義中使用“初始化方法”屬性。 類似地,實現[`org.springframework.beans.factory.DisposableBean`](http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/beans/factory/DisposableBean.html)接口允許 Bean 在包含它的容器被銷毀時獲得回調。 `DisposableBean`接口指定一種方法: `DisposableBean.java` ```java void destroy() throws Exception; A sample bean implementing above interfaces would look like this: package com.howtodoinjava.task; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class DemoBean implements InitializingBean, DisposableBean { //Other bean attributes and methods @Override public void afterPropertiesSet() throws Exception { //Bean initialization code } @Override public void destroy() throws Exception { //Bean destruction code } } ``` #### 2.2. 了解特定行為的接口 Spring 提供了一系列`*Aware`接口,這些接口允許 bean 向容器指示它們需要一定的基礎結構依賴。 每個接口都將要求您實現一種將依賴項注入 bean 的方法。 這些接口可以概括為: | 感知接口 | 覆蓋方法 | 目的 | | --- | --- | --- | | `ApplicationContextAware` | `void setApplicationContext(ApplicationContext applicationContext) throws BeansException;` | 希望由其運行所要通知的`ApplicationContext`的任何對象實現的接口。 | | `ApplicationEventPublisherAware` | `void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);` | 設置運行該對象的`ApplicationEventPublisher`。 | | `BeanClassLoaderAware` | `void setBeanClassLoader(ClassLoader classLoader);` | 將 bean 類加載器提供給 bean 實例的回調。 | | `BeanFactoryAware` | `void setBeanFactory(BeanFactory beanFactory) throws BeansException;` | 將擁有的工廠提供給 Bean 實例的回調。 | | `BeanNameAware` | `void setBeanName(String name);` | 在創建此 bean 的 bean 工廠中設置 bean 的名稱。 | | `BootstrapContextAware` | `void setBootstrapContext(BootstrapContext bootstrapContext);` | 設置該對象在其中運行的 BootstrapContext。 | | `LoadTimeWeaverAware` | `void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver);` | 設置此對象包含 ApplicationContext 的 LoadTimeWeaver。 | | `MessageSourceAware` | `void setMessageSource(MessageSource messageSource);` | 設置此對象在其中運行的 MessageSource。 | | `NotificationPublisherAware` | `void setNotificationPublisher(NotificationPublisher publisher);` | 為當前的托管資源實例設置`NotificationPublisher`實例。 | | `PortletConfigAware` | `void setPortletConfig(PortletConfig portletConfig);` | 設置運行該對象的 PortletConfig。 | | `PortletContextAware` | `void setPortletContext(PortletContext portletContext);` | 設置此對象在其中運行的 PortletContext。 | | `ResourceLoaderAware` | `void setResourceLoader(ResourceLoader resourceLoader);` | 設置運行該對象的 ResourceLoader。 | | `ServletConfigAware` | `void setServletConfig(ServletConfig servletConfig);` | 設置運行該對象的 ServletConfig。 | | `ServletContextAware` | `void setServletContext(ServletContext servletContext);` | 設置運行該對象的 ServletContext。 | Java 程序展示了使用感知接口來控制字符串 bean 生命周期的用法。 `DemoBean.java` ```java package com.howtodoinjava.task; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.weaving.LoadTimeWeaverAware; import org.springframework.core.io.ResourceLoader; import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.jmx.export.notification.NotificationPublisher; import org.springframework.jmx.export.notification.NotificationPublisherAware; public class DemoBean implements ApplicationContextAware, ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, LoadTimeWeaverAware, MessageSourceAware, NotificationPublisherAware, ResourceLoaderAware { @Override public void setResourceLoader(ResourceLoader arg0) { // TODO Auto-generated method stub } @Override public void setNotificationPublisher(NotificationPublisher arg0) { // TODO Auto-generated method stub } @Override public void setMessageSource(MessageSource arg0) { // TODO Auto-generated method stub } @Override public void setLoadTimeWeaver(LoadTimeWeaver arg0) { // TODO Auto-generated method stub } @Override public void setBeanName(String arg0) { // TODO Auto-generated method stub } @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub } @Override public void setBeanClassLoader(ClassLoader arg0) { // TODO Auto-generated method stub } @Override public void setApplicationEventPublisher(ApplicationEventPublisher arg0) { // TODO Auto-generated method stub } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub } } ``` #### 2.3. 自定義`init()`和`destroy()`方法 可以通過兩種方式定義 bean 配置文件中的默認`init`和`destroy`方法: * **Bean 本地定義**適用于單個 Bean * **全局定義**適用于 bean 上下文中定義的所有 bean ##### 2.3.1. Bean 本地定義 本地定義如下。 `beans.xml` ```java <beans> <bean id="demoBean" class="com.howtodoinjava.task.DemoBean" init-method="customInit" destroy-method="customDestroy"></bean> </beans> ``` ##### 2.3.2. 全局定義 全局定義如下。 這些方法將為`<beans>`標簽下給出的所有 bean 定義調用。 當您具有為所有 bean 一致定義通用方法名稱(例如`init()`和`destroy()`)的模式時,它們很有用。 此功能可幫助您不必為所有 bean 單獨提及`init`和`destroy`方法名稱。 ```java <beans default-init-method="customInit" default-destroy-method="customDestroy"> <bean id="demoBean" class="com.howtodoinjava.task.DemoBean"></bean> </beans> ``` 顯示在 bean XML 配置文件中配置的方法的 Java 程序。 `DemoBean.java` ```java package com.howtodoinjava.task; public class DemoBean { public void customInit() { System.out.println("Method customInit() invoked..."); } public void customDestroy() { System.out.println("Method customDestroy() invoked..."); } } ``` #### 2.4. `@PostConstruct`和`@PreDestroy` 從 Spring 2.5 開始,您還可以使用注解來指定使用`@PostConstruct`和`@PreDestroy`注解的生命周期方法。 * `@PostConstruct`帶注解的方法將在使用默認構造函數構造 Bean 之后,并且在實例返回給請求對象之前被調用。 * 在即將在 bean 容器內銷毀 bean 之前,將調用`@PreDestroy`帶注解的方法。 Java 程序,用于顯示**注解配置**的使用以控制注解的使用。 ```java package com.howtodoinjava.task; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class DemoBean { @PostConstruct public void customInit() { System.out.println("Method customInit() invoked..."); } @PreDestroy public void customDestroy() { System.out.println("Method customDestroy() invoked..."); } } ``` 因此,這一切都與 Spring 容器內的 **spring bean 生命周期**有關。 記住給定的生命周期事件類型,這是一個常見的問題[Spring 面試問題](https://howtodoinjava.com/interview-questions/top-spring-interview-questions-with-answers/)。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看