詳解Spring中InitializingBean接口的功能
Spring的InitializingBean接口有很好的用處,位于spring beans中,它只提供一個方法afterPropertiesSet(),當(dāng)你實現(xiàn)了該方法后,spring就會對你提供框架級的支持:當(dāng)你通過sring容器生產(chǎn)出實現(xiàn)了該接口的類的實例后,它就會調(diào)用afterPropertiesSet方法,通過這個方法,你可以檢查你的bean是否正確地被初始化了.當(dāng)然,你也可以用init-method方法.這兩種方式可以同時使用,調(diào)用的順序為init-method后調(diào)用.
下面介紹下Spring中InitializingBean接口的功能,內(nèi)容如下所示:
如果你將類交給Spring容器管理,但是需要Spring幫你運(yùn)行初始化方法
此時我們可以借助InitializingBean接口實現(xiàn)初始化方法的效果
InitializingBean接口的原理:
Spring實例化一個類后,會調(diào)用類中的afterPropertiesSet方法,達(dá)到初始化初始化方法的目的
下文筆者講述Spring中InitializingBean接口的功能簡介說明,如下所示
InitializingBean接口的功能
InitializingBean接口
為bean提供了初始化方法的方式
這個接口中只包括afterPropertiesSet方法
凡是繼承該接口的類
在初始化bean的時,都會運(yùn)行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"); } } -----運(yùn)行以上代碼,將輸出以下信息--------- 啟動時自動執(zhí)行 afterPropertiesSet... init method...
到此這篇關(guān)于Spring中InitializingBean接口的功能的文章就介紹到這了,更多相關(guān)Spring InitializingBean接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Lambda表達(dá)式查找list集合中是否包含某值問題
Java使用Lambda表達(dá)式查找list集合中是否包含某值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06SpringSecurity OAuth2單點登錄和登出的實現(xiàn)
本文主要介紹了SpringSecurity OAuth2單點登錄和登出的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Java?spring?boot實現(xiàn)批量刪除功能詳細(xì)示例
這篇文章主要給大家介紹了關(guān)于Java?spring?boot實現(xiàn)批量刪除功能的相關(guān)資料,文中通過代碼以及圖文將實現(xiàn)的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-08-08關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決
這篇文章主要介紹了關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08