亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Spring中InitializingBean接口的功能

 更新時間:2022年05月05日 14:11:00   作者:java265  
這篇文章主要介紹了Spring中InitializingBean接口的功能,講述了spring中InitializingBean接口的功能簡介說明,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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集合中是否包含某值問題

    Java使用Lambda表達(dá)式查找list集合中是否包含某值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • SpringSecurity OAuth2單點登錄和登出的實現(xiàn)

    SpringSecurity OAuth2單點登錄和登出的實現(xiàn)

    本文主要介紹了SpringSecurity OAuth2單點登錄和登出的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java多線程按指定順序同步執(zhí)行

    Java多線程按指定順序同步執(zhí)行

    這篇文章主要介紹了java多線程如何按指定順序同步執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Spring MVC實現(xiàn)一次簡單的CRUD示例

    Spring MVC實現(xiàn)一次簡單的CRUD示例

    這篇文章主要介紹了Spring MVC實現(xiàn)一次簡單的CRUD示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Java線程死鎖實例及解決方法

    Java線程死鎖實例及解決方法

    這篇文章主要介紹了Java線程死鎖實例及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 解決@SpringBootTest 單元測試遇到的坑

    解決@SpringBootTest 單元測試遇到的坑

    這篇文章主要介紹了解決@SpringBootTest 單元測試遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java?spring?boot實現(xiàn)批量刪除功能詳細(xì)示例

    Java?spring?boot實現(xiàn)批量刪除功能詳細(xì)示例

    這篇文章主要給大家介紹了關(guān)于Java?spring?boot實現(xiàn)批量刪除功能的相關(guān)資料,文中通過代碼以及圖文將實現(xiàn)的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-08-08
  • Java并發(fā)編程之阻塞隊列詳解

    Java并發(fā)編程之阻塞隊列詳解

    這篇文章主要為大家詳細(xì)介紹了Java并發(fā)編程之阻塞隊列,什么是阻塞隊列?主要的阻塞隊列及其方法介紹,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決

    關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決

    這篇文章主要介紹了關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java內(nèi)部類詳解

    Java內(nèi)部類詳解

    內(nèi)部類在 Java 里面算是非常常見的一個功能了,在日常開發(fā)中我們肯定多多少少都用過,這里總結(jié)一下關(guān)于 Java 中內(nèi)部類的相關(guān)知識點和一些使用內(nèi)部類時需要注意的點。
    2020-02-02

最新評論