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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - spring之Bean的生命周期詳解

spring之Bean的生命周期詳解

2020-10-05 12:33阿木俠 Java教程

本篇文章主要介紹了spring之Bean的生命周期詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Bean的生命周期:

Bean的定義——Bean的初始化——Bean的使用——Bean的銷毀

Bean的定義

Bean 是 spring 裝配的組件模型,一切實體類都可以配置成一個 Bean ,進而就可以在任何其他的 Bean 中使用,一個 Bean 也可以不是指定的實體類,這就是抽象 Bean 。

Bean的初始化

Spring中bean的初始化回調(diào)有兩種方法

一種是在配置文件中聲明init-method="init",然后在一個實體類中用init()方法來初始化

另一種是實現(xiàn)InitializingBean接口,覆蓋afterPropertiesSet()方法。

第一種:

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="init-one" class="org.spring.test.BeanInitDemo1" init-method="init">
    <property name="message" value="這里是配置文件中為message賦值"></property>
  </bean>
</beans>

BeanInitDemo1類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.spring.test;
 
public class BeanInitDemo1 {
  private String message;
 
  public String getMessage() {
    return message;
  }
 
  public void setMessage(String message) {
    this.message = message;
  }
  public void init(){
    this.setMessage("這里是init()方法初始化設(shè)值");
  }
}

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.spring.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class Test {
 
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BeanInitDemo1 bid = (BeanInitDemo1) context.getBean("init-one");
    System.out.println(bid.getMessage());
  }
 
}

運行結(jié)果:

這里是init()方法初始化設(shè)值

原因:init()初始化方法的調(diào)用是在配置文件的Bean初始化之后執(zhí)行的, 所以改變了配置文件中對message的賦值。

第二種:

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   
  <bean id="init-two" class="org.spring.test.BeanInitDemo2">
    <property name="message" value="這里是配置文件中為message賦值"></property>
  </bean>
</beans>

編寫B(tài)eanInitDemo2類,使其實現(xiàn)InitializingBean接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.spring.test;
 
import org.springframework.beans.factory.InitializingBean;
 
public class BeanInitDemo2 implements InitializingBean{
  private String message;
 
  public String getMessage() {
    return message;
  }
 
  public void setMessage(String message) {
    this.message = message;
  }
 
  public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub
    this.setMessage("這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值");
  }
   
}

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.spring.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class Test {
 
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BeanInitDemo2 bid = (BeanInitDemo2) context.getBean("init-two");
    System.out.println(bid.getMessage());
  }
 
}

運行結(jié)果: 這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值

原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后執(zhí)行,所以改變了配置文件中對message的賦值

Bean的使用

Spring中有兩種使用bean的方法:

1, BeanFactory:

?
1
2
BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml"));
factory.getBean("student");

BeanFactory是延遲加載,如果Bean的某一個屬性沒有注入,BeanFacotry加載后,直至第一次使用getBean方法才會拋出異常,也就是說當使用BeanFactory實例化對象時,配置的bean不會馬上被實例化。當你使用該bean時才會被實例化(getBean)。

2, ApplicationContext:

?
1
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

如果使用ApplicationContext,則配置的bean如果是singleton不管你用還是不用,都被實例化。ApplicationContext在初始化自身時檢驗,這樣有利于檢查所依賴屬性是否注入。ApplicationContext是BeanFactory的子類,除了具有BeanFactory的所有功能外還提供了更完整的框架功能,例如國際化,資源訪問等。所以通常情況下我們選擇使用ApplicationContext。

Bean的銷毀

Bean的銷毀和初始化一樣,都是提供了兩個方法

一是在配置文件中聲明destroy-method="cleanup",然后在類中寫一個cleanup()方法銷毀

二是實現(xiàn)DisposableBean接口,覆蓋destory()方法

第一種:

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="destory-one" class="org.spring.test.BeanDestoryDemo1" destroy-method="cleanup">
    <property name="message" value="這里是配置文件中為message賦值"></property>
  </bean>
</beans>

BeanDestoryDemo1類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.spring.test;
 
public class BeanDestoryDemo1 {
  private String message;
 
  public String getMessage() {
    return message;
  }
 
  public void setMessage(String message) {
    this.message = message;
  }
  public void cleanup(){
    System.out.println("銷毀之前可以調(diào)用一些方法");
  }
}

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package org.spring.test;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DestortTest {
  public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean("destory-one");
    System.out.println(bdd.getMessage());
    context.registerShutdownHook();
  }
}

運行結(jié)果:

spring之Bean的生命周期詳解

context.registerShutdownHook()是為spring注冊關(guān)閉吊鉤,程序退出之前關(guān)閉spring容器,如果沒有

context.registerShutdownHook();將不會執(zhí)行cleanup()方法。

第二種:

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="destory-two" class="org.spring.test.BeanDestoryDemo2">
    <property name="message" value="這里是配置文件中為message賦值"></property>
  </bean>
</beans

BeanDestoryDemo2類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.spring.test;
 
import org.springframework.beans.factory.DisposableBean;
 
public class BeanDestoryDemo2 implements DisposableBean{
  private String message;
 
  public String getMessage() {
    return message;
  }
 
  public void setMessage(String message) {
    this.message = message;
  }
 
  public void destroy() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("同樣,銷毀之前調(diào)用的方法");
  }
}

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package org.spring.test;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DestortTest {
  public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean("destory-two");
    System.out.println(bdd.getMessage());
    context.registerShutdownHook();
  }

運行結(jié)果:

spring之Bean的生命周期詳解

Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及銷毀之前可以做一些工作,更靈活的管理Bean。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/weixin_36380516/article/details/72354668?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲AV中文字幕无码久久 | 天天爱综合网 | 日韩精品成人免费观看 | 国产日韩精品一区二区在线观看 | 国产精品免费一级在线观看 | freesex1718处xx| 亚洲国产精品自在自线观看 | 女被男啪到哭 | 亚洲精品影视 | 国产精品欧美亚洲韩国日本99 | 国产精品免费拍拍拍 | 亚洲一区二区福利视频 | 婷婷福利| 办公室操秘书 | 国产福利不卡视频在免费 | 岛国在线播放v片免费 | 青草青草视频 | 青草视频免费观看 | h视频免费高清在线观看 | 国产一级片免费视频 | 情欲综合网 | 国产乱码免费卡1卡二卡3卡四 | 爱情岛论坛亚洲品质自拍视频 | 欧美一级视频免费观看 | 亚洲国产福利精品一区二区 | 亚洲日本中文字幕天天更新 | 欧美日本一道高清二区三区 | 亚洲欧美精品一区二区 | 欧美vpswindowssex 欧美va在线高清 | 日韩精品成人免费观看 | 97超pen个人视频公开视频视 | 日韩一区二区三区四区五区 | 91传媒制片厂果冻有限公司 | 亚洲va欧美va国产综合久久 | 男人的天堂在线观看视频不卡 | 亚洲成a人片777777久久 | 亚欧有色在线观看免费版高清 | 国产精品成 | 成年人视频在线免费看 | 182免费在线观看 | 成人特级毛片69免费观看 |