欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring學習筆記之bean生命周期

 更新時間:2017年12月13日 11:21:47   作者:小崔  
Spring Bean是Spring應用中最最重要的部分了。下面這篇文章主要給大家介紹了關于Spring學習筆記之bean生命周期的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

上一篇文章主要學習了下bean的配置、注入、自定義屬性編輯器,今天來熟悉bean的生命周期。

任何一個事物都有自己的生命周期,生命的開始、生命中、生命結(jié)束。大家最熟悉的應該是servlet 的生命周期吧。和 servlet 一樣 spring bean 也有自己的生命周期。

在開發(fā)中生命周期是一個很常見的名詞,基本每種編程語言都能找到與它關聯(lián)的。關于bean的生命周期我在網(wǎng)上也找了好多,基本都差不多。這里我主要是想通過代碼來驗證,畢竟學的知識都是一樣的,都是學的Java,最重要的是動手練習,這樣印象更深。

下面是它生命周期的描述,我們通過demo來進行驗證。

下圖是它執(zhí)行的順序。

一、創(chuàng)建LiftCycle類實現(xiàn)BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware5個接口方法

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{
 
 private String name;
 public String getName() {
 System.out.println("getName name="+name);
 return name;
 }
 public void setName(String name) {
  System.out.println("setName name="+name);
 this.name = name;
 }
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
  System.out.println("InitializingBean.afterPropertiesSet()");
 }
 public void setBeanName(String arg0) {
 // TODO Auto-generated method stub
 System.out.println("BeanNameAware.setBeanName");
 }
 public void setBeanFactory(BeanFactory arg0) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanFactoryAware.setBeanFactory");
 }
 public void destroy() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("DisposableBean.destroy");
 }
 public void myInit() {
 System.out.println("【init-method】調(diào)用<bean>的init-method屬性指定的初始化方法");
 }
 public void myDestory() {
 System.out.println("【destroy-method】調(diào)用<bean>的destroy-method屬性指定的初始化方法");
 }
 public void setApplicationContext(ApplicationContext arg0) throws BeansException {
 // TODO Auto-generated method stub
  System.out.println("ApplicationContextAware.setApplicationContext");
 } 
}

二、注冊InstantiationAwareBeanPostProcessor接口

package Cuiyw.Spring.Service;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization");
 return bean;
 }
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization");
 return bean;
 }
 public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
 return true;
 }
 public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
 return null;
 }
 public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
  String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
 return pvs;
 }
}

三、注冊BeanPostProcessor接口

其實InstantiationAwareBeanPostProcessor繼承BeanPostProcessor,所以在上面我也實現(xiàn)了BeanPostProcessor接口的方法

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanPostProcessor.postProcessAfterInitialization ");
 return bean;
 }
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
 return bean;
 }
}

四、注冊BeanFactoryPostProcessor接口

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
 public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory");
 }
}

五、在上下文中配置

這里還是在上一個博客demo的基礎上進行修改,為了有其他干擾,我先把service去掉了。

<?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.xsd">
<bean id="beanPostProcessor" class="Cuiyw.Spring.Service.MyBeanPostProcessor"></bean>
<bean id="instantiationAwareBeanPostProcessor" class="Cuiyw.Spring.Service.MyInstantiationAwareBeanPostProcessor"></bean>
<bean id="beanFactoryPostProcessor" class="Cuiyw.Spring.Service.MyBeanFactoryPostProcessor"></bean>
<bean id="lifeCycle" class="Cuiyw.Spring.Service.LifeCycle" init-method="myInit" destroy-method="myDestory">
<property name="name" value="cuiyw1"></property>
</bean>
</beans>

六、在main中使用bean

package Cuiyw.SpringAop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Cuiyw.Spring.IService.IService;
import Cuiyw.Spring.Service.LifeCycle;

public class App 
{
 public static void main( String[] args )
 {
 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
 BeanFactory factory=context;
 LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class);
 lifeCycle.setName("cuiyw2");
  System.out.println("lifeCycle.name="+lifeCycle.getName());
 ((ClassPathXmlApplicationContext)factory).registerShutdownHook(); 
 /*service=(IService)factory.getBean("ServiceA");
 service.service("Cuiyw ServiceA"); 
 service=(IService)factory.getBean("ServiceImpl");
 service.service("Cuiyw ServiceImpl"); */
 }
}

七、輸入打印結(jié)果

可以發(fā)現(xiàn)輸出的順序和上面圖的順序基本一致。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • Mybatis延遲加載原理和延遲加載配置詳解

    Mybatis延遲加載原理和延遲加載配置詳解

    這篇文章主要介紹了Mybatis延遲加載原理和延遲加載配置詳解,MyBatis中的延遲加載,也稱為懶加載,是指在進行表的關聯(lián)查詢時,按照設置延遲規(guī)則推遲對關聯(lián)對象的select查詢,需要的朋友可以參考下
    2023-10-10
  • Maven安裝與配置及Idea配置Maven的全過程

    Maven安裝與配置及Idea配置Maven的全過程

    Maven是一個項目管理工具,可以對Java項目進行自動化的構(gòu)建和依賴管理,下面這篇文章主要給大家介紹了關于Maven安裝與配置及Idea配置Maven的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • fastJson泛型如何轉(zhuǎn)換的實現(xiàn)

    fastJson泛型如何轉(zhuǎn)換的實現(xiàn)

    這篇文章主要介紹了fastJson泛型如何轉(zhuǎn)換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Springboot實現(xiàn)前后端分離excel下載

    Springboot實現(xiàn)前后端分離excel下載

    這篇文章主要介紹了Springboot實現(xiàn)前后端分離excel下載,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java使用EasyExcel模版導出詳細操作教程

    Java使用EasyExcel模版導出詳細操作教程

    業(yè)務中經(jīng)常需要按照一個特定的模板導出特定內(nèi)容,有些單元格還要求特殊的格式,所以下面這篇文章主要給大家介紹了關于Java使用EasyExcel模版導出的相關資料,需要的朋友可以參考下
    2023-10-10
  • 2020最新eclipse安裝過程及細節(jié)

    2020最新eclipse安裝過程及細節(jié)

    這篇文章主要介紹了2020最新eclipse安裝過程及細節(jié),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • hibernate-validator如何使用校驗框架

    hibernate-validator如何使用校驗框架

    高效、合理的使用hibernate-validator校驗框架可以提高程序的可讀性,以及減少不必要的代碼邏輯,本文主要介紹了hibernate-validator如何使用校驗框架,感興趣的可以了解一下
    2022-04-04
  • Springboot整合ActiveMQ實現(xiàn)消息隊列的過程淺析

    Springboot整合ActiveMQ實現(xiàn)消息隊列的過程淺析

    昨天仔細研究了activeMQ消息隊列,也遇到了些坑,下面這篇文章主要給大家介紹了關于SpringBoot整合ActiveMQ的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • SpringBoot實現(xiàn)微信及QQ綁定登錄的示例代碼

    SpringBoot實現(xiàn)微信及QQ綁定登錄的示例代碼

    本文主要介紹了SpringBoot實現(xiàn)微信及QQ綁定登錄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • 解決SpringBoot引用別的模塊無法注入的問題

    解決SpringBoot引用別的模塊無法注入的問題

    這篇文章主要介紹了解決SpringBoot引用別的模塊無法注入的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論