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

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

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

1.InitializingBean

該接口的作用是:

允許一個bean在它的所有必須屬性被BeanFactory設置后,來執(zhí)行初始化的工作,該接口中只有一個方法,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的時候獲得一次回調。

DisposableBean接口也只規(guī)定了一個方法: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.案例演示

/**
 * 實現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 被實例化");
	}

	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("設置:"+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>

測試代碼

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

輸出結果:

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

通過輸出能夠顯示spring初始化bean的時候,如果bean實現了InitializingBean接口,會自動調用afterPropertiesSet方法,在bean被銷毀的時候如果實現了DisposableBean接口會自動回調destroy方法后然后再銷毀

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java獲取網絡圖片上傳到OSS的方法

    java獲取網絡圖片上傳到OSS的方法

    這篇文章主要為大家詳細介紹了java獲取網絡圖片上傳到OSS,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Jdk1.8 HashMap實現原理詳細介紹

    Jdk1.8 HashMap實現原理詳細介紹

    這篇文章主要介紹了Jdk1.8 HashMap實現原理詳細介紹的相關資料,需要的朋友可以參考下
    2016-12-12
  • Java對接阿里云短信服務保姆級教程(新手秒會)

    Java對接阿里云短信服務保姆級教程(新手秒會)

    這篇文章主要介紹了如何在阿里云上申請短信服務以及如何使用Java代碼進行對接,包括申請資質、簽名和模板,以及編寫Java代碼整合成工具類進行調用的步驟,需要的朋友可以參考下
    2024-12-12
  • jdk源碼閱讀Collection詳解

    jdk源碼閱讀Collection詳解

    這篇文章主要介紹了jdk源碼閱讀Collection詳解,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • Spring中的spring-retry重試機制解析

    Spring中的spring-retry重試機制解析

    這篇文章主要介紹了Spring中的spring-retry重試機制解析,spring-retry可以通過注解,在不入侵原有業(yè)務邏輯代碼的方式下,優(yōu)雅的實現重處理功能,在spring-retry中,所有配置都是基于簡單注釋的,需要的朋友可以參考下
    2024-01-01
  • 解決Eclipse的Servers視圖中無法添加Tomcat6/Tomcat7的方法

    解決Eclipse的Servers視圖中無法添加Tomcat6/Tomcat7的方法

    這篇文章主要介紹了解決Eclipse的Servers視圖中無法添加Tomcat6/Tomcat7的方法的相關資料,需要的朋友可以參考下
    2017-02-02
  • SpringBoot yml配置文件調用過程解析

    SpringBoot yml配置文件調用過程解析

    這篇文章主要介紹了SpringBoot yml配置文件調用過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • SpringMVC中轉發(fā)與重定向的區(qū)別淺析

    SpringMVC中轉發(fā)與重定向的區(qū)別淺析

    這篇文章主要給大家介紹了關于SpringMVC中轉發(fā)與重定向的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 基于Spark實現隨機森林代碼

    基于Spark實現隨機森林代碼

    這篇文章主要為大家詳細介紹了基于Spark實現隨機森林代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 最全總結SpringBean的作用域管理

    最全總結SpringBean的作用域管理

    今天給大家詳細總結了SpringBean的作用域管理,文中有非常詳細的圖文介紹以及代碼示例,對正在學習java的小伙伴們還很有幫助,需要的朋友可以參考下
    2021-05-05

最新評論