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

Spring之InitializingBean接口和DisposableBean接口的使用

 更新時(shí)間:2024年01月13日 10:36:23   作者:波波烤鴨  
這篇文章主要介紹了Spring之InitializingBean接口和DisposableBean接口的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1.InitializingBean

該接口的作用是:

允許一個(gè)bean在它的所有必須屬性被BeanFactory設(shè)置后,來(lái)執(zhí)行初始化的工作,該接口中只有一個(gè)方法,afterPropertiesSet

public interface InitializingBean {

	/**
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 * @throws Exception in the event of misconfiguration (such as failure to set an
	 * essential property) or if initialization fails for any other reason
	 */
	void afterPropertiesSet() throws Exception;

}

2.DisposableBean

該接口的作用是:允許在容器銷毀該bean的時(shí)候獲得一次回調(diào)。

DisposableBean接口也只規(guī)定了一個(gè)方法:destroy

public interface DisposableBean {

	/**
	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
	 * @throws Exception in case of shutdown errors. Exceptions will get logged
	 * but not rethrown to allow other beans to release their resources as well.
	 */
	void destroy() throws Exception;

}

3.案例演示

/**
 * 實(shí)現(xiàn)InitializingBean和DisposableBean接口
 * @author dengp
 *
 */
public class User implements InitializingBean,DisposableBean{

	private int id;
	
	private String name;
	
	private String beanName;
	
	public User(){
		System.out.println("User 被實(shí)例化");
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		System.out.println("設(shè)置:"+name);
		this.name = name;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", beanName=" + beanName + "]";
	}

	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("destory ....");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet....");
	}
}

配置文件

<?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 class="com.dpb.pojo.User" id="user" >
		<property name="name" value="波波烤鴨"></property>
	</bean>
	
</beans>

測(cè)試代碼

@Test
public void test1() {
	ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	User user = ac.getBean(User.class);
	System.out.println(user);
	ac.registerShutdownHook();
}

輸出結(jié)果:

User 被實(shí)例化
設(shè)置:波波烤鴨
afterPropertiesSet....
User [id=0, name=波波烤鴨, beanName=null]
destory ....

通過(guò)輸出能夠顯示spring初始化bean的時(shí)候,如果bean實(shí)現(xiàn)了InitializingBean接口,會(huì)自動(dòng)調(diào)用afterPropertiesSet方法,在bean被銷毀的時(shí)候如果實(shí)現(xiàn)了DisposableBean接口會(huì)自動(dòng)回調(diào)destroy方法后然后再銷毀

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入了解Java虛擬機(jī)棧以及內(nèi)存模型

    深入了解Java虛擬機(jī)棧以及內(nèi)存模型

    這篇文章主要介紹了深入了解Java虛擬機(jī)棧以及內(nèi)存模型,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很大的幫助,需要的朋友可以參考下
    2021-04-04
  • Java?Lambda表達(dá)式常用的函數(shù)式接口

    Java?Lambda表達(dá)式常用的函數(shù)式接口

    這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對(duì)常用的函數(shù)式接口的介紹,具有一的的參考價(jià)值需要的小伙伴可以參考一下
    2022-04-04
  • SpringBoot前后端分離跨域問(wèn)題:狀態(tài)碼403拒絕訪問(wèn)解決辦法

    SpringBoot前后端分離跨域問(wèn)題:狀態(tài)碼403拒絕訪問(wèn)解決辦法

    這篇文章主要給大家介紹了關(guān)于SpringBoot前后端分離跨域問(wèn)題:狀態(tài)碼403拒絕訪問(wèn)的解決辦法,403是被服務(wù)器拒絕了,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • java后端解決跨域的幾種問(wèn)題解決

    java后端解決跨域的幾種問(wèn)題解決

    這篇文章主要介紹了java后端解決跨域的幾種問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java?-jar參數(shù)設(shè)置小結(jié)

    Java?-jar參數(shù)設(shè)置小結(jié)

    本文主要介紹了Java?-jar參數(shù)設(shè)置小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 使用Java實(shí)現(xiàn)轉(zhuǎn)換掃描的文檔為可搜索的PDF

    使用Java實(shí)現(xiàn)轉(zhuǎn)換掃描的文檔為可搜索的PDF

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)轉(zhuǎn)換掃描的文檔為可搜索的PDF,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標(biāo)和包名

    java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標(biāo)和包名

    這篇文章主要介紹了java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標(biāo)和包名,主要用在做主題時(shí)替換這些常見應(yīng)用的圖片,需要的朋友可以參考下
    2014-06-06
  • java實(shí)現(xiàn)飲料自助售貨機(jī)

    java實(shí)現(xiàn)飲料自助售貨機(jī)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飲料自助售貨機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Netty實(shí)現(xiàn)簡(jiǎn)易版的RPC框架過(guò)程詳解

    Netty實(shí)現(xiàn)簡(jiǎn)易版的RPC框架過(guò)程詳解

    這篇文章主要為大家介紹了Netty實(shí)現(xiàn)簡(jiǎn)易版的RPC框架過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Java的三種代理模式簡(jiǎn)述

    Java的三種代理模式簡(jiǎn)述

    這篇文章主要簡(jiǎn)述Java的三種代理模式,java的代理模式主要包括靜態(tài)代理、動(dòng)態(tài)代理、Cglib代理,感興趣的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09

最新評(píng)論