深入理解Spring中bean的生命周期介紹
1.以ApplocationContext上下文單例模式裝配bean為例,深入探討bean的生命周期:
(1).生命周期圖:
(2).具體事例:
person類(lèi)實(shí)現(xiàn)BeanNameAware,BeanFactoryAware接口
public class Person implements BeanNameAware ,BeanFactoryAware{ private String name; public Person(){ System.out.println("調(diào)用構(gòu)造器為屬性值初始化"); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void setBeanName(String arg0) { // TODO Auto-generated method stub System.out.println("獲取beanName id值"+" "+arg0); } @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("獲取BeanFactory" +" "+arg0); } }
public class MyBeanPostProcessor implements BeanPostProcessor{ @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("調(diào)用postProcessAfterInitialization"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("調(diào)用postProcessBeforeInitialization"); return arg0; } }
ApplicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean的配置文件 --> <bean id="person" class="org.jingdong.bean.life.Person"> <property name="name" value="grl"></property> </bean> <bean id="myBeanPostProcessor" class="org.jingdong.bean.life.MyBeanPostProcessor"></bean> </beans>
Main.java
public class Main { public static void main(String[] args) { // 創(chuàng)建IOC容器 ApplicationContext ac = new ClassPathXmlApplicationContext("org/jingdong/bean/life/applicationContext.xml"); //從容器中獲取bean實(shí)例 Person person = (Person) ac.getBean("person"); //使用bean System.out.println(person.getName()); } }
2.以Spring Factory裝配bean為例:
(1).生命周期圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot替換默認(rèn)的tomcat服務(wù)器的方法
Tomcat是Apache基金下的一個(gè)輕量級(jí)的Servlet容器,支持Servlet和JSP,Tomcat具有Web服務(wù)器特有的功能,在SpringBoot框架中,我們使用最多的是Tomcat,這是SpringBoot默認(rèn)的容器技術(shù),本文給大家介紹了Spring?Boot如何替換默認(rèn)的tomcat服務(wù)器,需要的朋友可以參考下2024-08-08java使用stream判斷兩個(gè)list元素的屬性并輸出方式
這篇文章主要介紹了java使用stream判斷兩個(gè)list元素的屬性并輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06使用SpringCache進(jìn)行緩存數(shù)據(jù)庫(kù)查詢(xún)方式
這篇文章主要介紹了使用SpringCache進(jìn)行緩存數(shù)據(jù)庫(kù)查詢(xún)方式,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10spring通過(guò)jdbc連接數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了spring通過(guò)jdbc連接數(shù)據(jù)庫(kù)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Java設(shè)計(jì)模式之依賴(lài)倒轉(zhuǎn)原則精解
設(shè)計(jì)模式(Design pattern)代表了最佳的實(shí)踐,通常被有經(jīng)驗(yàn)的面向?qū)ο蟮能浖_(kāi)發(fā)人員所采用。設(shè)計(jì)模式是軟件開(kāi)發(fā)人員在軟件開(kāi)發(fā)過(guò)程中面臨的一般問(wèn)題的解決方案。本篇介紹設(shè)計(jì)模式七大原則之一的依賴(lài)倒轉(zhuǎn)原則2022-02-02springboot如何通過(guò)不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類(lèi)
這篇文章主要介紹了springboot如何通過(guò)不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類(lèi),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)
這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05