最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • RocketMQ事務(wù)消息原理與使用詳解

    RocketMQ事務(wù)消息原理與使用詳解

    RocketMQ事務(wù)消息(Transactional Message)是指應(yīng)用本地事務(wù)和發(fā)送消息操作可以被定義到全局事務(wù)中,要么同時(shí)成功,要么同時(shí)失敗。RocketMQ的事務(wù)消息提供類似 X/Open XA 的分布式事務(wù)功能,通過(guò)事務(wù)消息能達(dá)到分布式事務(wù)的最終一致
    2023-02-02
  • 詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn)

    詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn)

    這篇文章主要介紹了詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java檢查字符串是否一致的四種方法

    Java檢查字符串是否一致的四種方法

    字符串比較是常見(jiàn)的操作,包括比較相等、比較大小、比較前綴和后綴串等,在 Java 中,比較字符串的常用方法有四個(gè):equals(),equalsIgnoreCase(),compareTo()和compareToIgnoreCase(),下面詳細(xì)介紹這四個(gè)方法的使用
    2024-04-04
  • SpringBoot原生組件注入實(shí)現(xiàn)兩種方式介紹

    SpringBoot原生組件注入實(shí)現(xiàn)兩種方式介紹

    SpringBoot是Spring全家桶的成員之一,基于約定優(yōu)于配置的思想(即有約定默認(rèn)值,在不配置的情況下會(huì)使用默認(rèn)值,在配置文件下配置的話會(huì)使用配置的值)。SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開(kāi)發(fā)的腳手架
    2022-10-10
  • MyBatis完成CRUD?詳細(xì)細(xì)節(jié)內(nèi)容剖析

    MyBatis完成CRUD?詳細(xì)細(xì)節(jié)內(nèi)容剖析

    這篇文章主要介紹了MyBatis完成CRUD?詳細(xì)細(xì)節(jié)內(nèi)容剖析,本文通過(guò)圖文示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-05-05
  • maven中下載jar包源碼和javadoc的命令介紹

    maven中下載jar包源碼和javadoc的命令介紹

    這篇文章主要介紹了maven中下載jar包源碼和javadoc的命令介紹,本文講解了Maven命令下載源碼和javadocs、通過(guò)配置文件添加、配置eclipse等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • SpringBoot3通過(guò)GraalVM生成exe執(zhí)行文件問(wèn)題

    SpringBoot3通過(guò)GraalVM生成exe執(zhí)行文件問(wèn)題

    文章介紹了如何安裝GraalVM和Visual Studio,并通過(guò)Spring Boot項(xiàng)目將Java應(yīng)用程序封裝成可執(zhí)行文件(.exe)
    2024-12-12
  • Spring聲明式事務(wù)管理從原理到實(shí)戰(zhàn)示例

    Spring聲明式事務(wù)管理從原理到實(shí)戰(zhàn)示例

    本文主要介紹了Spring聲明式事務(wù)管理的概念、實(shí)現(xiàn)原理、配置方法、異?;貪L機(jī)制、不同類的事務(wù)配置以及響應(yīng)式事務(wù)的支持,聲明式事務(wù)通過(guò)配置或注解簡(jiǎn)化了事務(wù)管理,使用AOP實(shí)現(xiàn),支持多種傳播行為和隔離級(jí)別,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼

    Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼

    這篇文章主要介紹了Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼,需要的朋友可以參考下
    2017-09-09
  • Java創(chuàng)建線程及配合使用Lambda方式

    Java創(chuàng)建線程及配合使用Lambda方式

    這篇文章主要介紹了Java創(chuàng)建線程及配合使用Lambda方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論

连云港市| 江城| 宣恩县| 永福县| 汉寿县| 千阳县| 渭南市| 河北省| 稷山县| 绥化市| 津南区| 仁寿县| 鹤岗市| 衡东县| 玛沁县| 富裕县| 曲阜市| 澜沧| 石狮市| 绥滨县| 青铜峡市| 陇川县| 宝应县| 安吉县| 阳春市| 塔河县| 商洛市| 陆良县| 沁水县| 贺州市| 永川市| 安西县| 汾阳市| 浦江县| 瑞昌市| 惠州市| 扶余县| 尼勒克县| 宜州市| 新疆| 谢通门县|