一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Spring學習筆記之bean生命周期

Spring學習筆記之bean生命周期

2021-03-04 10:09小崔 JAVA教程

Spring Bean是Spring應用中最最重要的部分了。下面這篇文章主要給大家介紹了關于Spring學習筆記之bean生命周期的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

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

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

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

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

下圖是它執行的順序。

Spring學習筆記之bean生命周期

Spring學習筆記之bean生命周期

一、創建liftcycle類實現beanfactoryaware,beannameaware,initializingbean,disposablebean,applicationcontextaware5個接口方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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】調用<bean>的init-method屬性指定的初始化方法");
 }
 public void mydestory() {
 system.out.println("【destroy-method】調用<bean>的destroy-method屬性指定的初始化方法");
 }
 public void setapplicationcontext(applicationcontext arg0) throws beansexception {
 // todo auto-generated method stub
  system.out.println("applicationcontextaware.setapplicationcontext");
 }
}

二、注冊instantiationawarebeanpostprocessor接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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,所以在上面我也實現了beanpostprocessor接口的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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接口

?
1
2
3
4
5
6
7
8
9
10
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去掉了。

?
1
2
3
4
5
6
7
8
9
10
11
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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"); */
 }
}

七、輸入打印結果

可以發現輸出的順序和上面圖的順序基本一致。

Spring學習筆記之bean生命周期

總結

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

原文鏈接:http://www.cnblogs.com/5ishare/p/8030038.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品乱码高清在线观看 | 国产精品青青青高清在线密亚 | 92精品国产成人观看免费 | 亚洲国产一区二区三区青草影视 | 国产老妇 | 女学生被老师调教在教室 | 美女校花被调教出奶水 | 午夜欧美精品 | 调教全程肉动画片在线观看 | 国产综合久久久久久 | 日韩毛片基地一区二区三区 | 成人福利在线视频免费观看 | 亚洲国产无线码在线观看 | a亚洲视频 | 男人天堂国产 | 日本又大又硬又粗的视频 | 爱欲荡漾在线观看 | 男男浴室吸乳play | 亚洲欧洲色图 | 欧美日韩国产一区二区三区不卡 | 黄瓜视频黄版 | 久久AV国产麻豆HD真实 | 亚洲AV无码A片在线观看蜜桃 | 操女人的b| 五月色婷婷久久综合 | 深夜免费在线观看 | 日本另类z0zx高清 | 男男playh片在线观看 | 丝袜护士强制脚足取精 | 2018生活片性色生活片 | 免看一级a一片成人123 | 国内精品 大秀视频 日韩精品 | 国产日韩精品一区二区在线观看 | 99久久伊人一区二区yy5099 | 国产123区在线视频观看 | 四虎永久免费地址ww417 | 日本国产高清色www视频在线 | 四虎网址| 好大好深好涨好烫还要 | 激情六月丁香婷婷四房播 | 精品蜜臀AV在线天堂 |