詳解Spring中InitializingBean接口的功能
Spring的InitializingBean接口有很好的用處,位于spring beans中,它只提供一個方法afterPropertiesSet(),當你實現了該方法后,spring就會對你提供框架級的支持:當你通過sring容器生產出實現了該接口的類的實例后,它就會調用afterPropertiesSet方法,通過這個方法,你可以檢查你的bean是否正確地被初始化了.當然,你也可以用init-method方法.這兩種方式可以同時使用,調用的順序為init-method后調用.
下面介紹下Spring中InitializingBean接口的功能,內容如下所示:
如果你將類交給Spring容器管理,但是需要Spring幫你運行初始化方法
此時我們可以借助InitializingBean接口實現初始化方法的效果
InitializingBean接口的原理:
Spring實例化一個類后,會調用類中的afterPropertiesSet方法,達到初始化初始化方法的目的
下文筆者講述Spring中InitializingBean接口的功能簡介說明,如下所示
InitializingBean接口的功能
InitializingBean接口
為bean提供了初始化方法的方式
這個接口中只包括afterPropertiesSet方法
凡是繼承該接口的類
在初始化bean的時,都會運行afterPropertiesSet方法
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
public class InitBean implements InitializingBean{
public void afterPropertiesSet() throws Exception {
System.out.println("啟動時自動執(zhí)行 afterPropertiesSet...");
}
public void init(){
System.out.println("init method...");
}
}
---配置文件-----
<?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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="true">
<bean id="initBean" class="com.java265.InitBean" init-method="init"> </bean>
</beans>
---main程序----
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new
FileSystemXmlApplicationContext("classpath:/applicationContext-core.xml");
context.getBean("initBean");
}
}
-----運行以上代碼,將輸出以下信息---------
啟動時自動執(zhí)行 afterPropertiesSet...
init method...到此這篇關于Spring中InitializingBean接口的功能的文章就介紹到這了,更多相關Spring InitializingBean接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot 集成pgsql+mybatis plus的詳細步驟
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步驟與 MyBatis 類似,只不過在 MyBatis Plus 中提供了更多的便利功能,如自動生成 SQL、分頁查詢、Wrapper 查詢等,下面分步驟給大家介紹springboot 集成pgsql+mybatis plus的過程,感興趣的朋友一起看看吧2023-12-12
在deepin上如何使用Fleet開發(fā)SpringBoot?3.0.0項目
這篇文章主要介紹了在deepin上使用Fleet開發(fā)SpringBoot?3.0.0項目的過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09

