SpringBoot如何使用applicationContext.xml配置文件
使用applicationContext.xml配置文件
SpringBoot默認(rèn)是通過(guò)Java代碼進(jìn)行依賴注入,但也為xml形式的依賴注入提供了入口,就是@ImportResource注解。
我們可以在SpringBoot的啟動(dòng)類上添加這個(gè)注解并在注解的locations屬性中指定xml配置文件。(可以使用一個(gè)文件集合也可以只引入主配置文件然后在主配置文件中使用標(biāo)簽引入其他子配置文件,個(gè)人更喜歡第二中方式)。
這樣容器在啟動(dòng)時(shí)配置在xml文件中的BeanDefination也可以被解析。
applicationContext 加載配置文件
ApplicationContext 理解為spring容器的上下文,通過(guò)上下文操作容器中bean.
ClassPathXmlApplicationContext
:加載classpath下的配置文件創(chuàng)建一個(gè)容器實(shí)例FileSystemXmlApplicationContext
: 加載文件系統(tǒng)中任意目錄下的配置文件,創(chuàng)建一個(gè)容器實(shí)例
案例
/*方式一 :ClassPathXmlApplicationContext*/ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml"); /*方式二 FileSystemXmlApplicationContext */ ?? ??? ?//FileSystemXmlApplicationContext ioc= new FileSystemXmlApplicationContext("E://1804_2//20180827spring//config//spring.xml"); ?? ??? ?User u = (User) ioc.getBean("user1"); ?? ??? ?System.out.println(u);
多文件的加載方法
/*方式一*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml,spring-mvc.xml"); /*方式二*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String[]{"spring.xml,spring-mvc.xml"}); /*方式三*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-*.xml"); /*方式四*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:spring-*.xml","mybatis.xml"}); /*方式五*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:*.xml"); /*方式六*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath*:*.xml"); /*方式七*/ //ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:*.xml","classpath:springmvc/beans.xml"});
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項(xiàng)目中PropertySource如何讀取yaml配置文件
這篇文章主要介紹了springboot項(xiàng)目中PropertySource如何讀取yaml配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01關(guān)于MyBatis Plus中使用or和and問(wèn)題
這篇文章主要介紹了關(guān)于MyBatis Plus中使用or和and問(wèn)題,需要的朋友可以參考下2020-12-12Intellij無(wú)法創(chuàng)建java文件解決方案
這篇文章主要介紹了Intellij無(wú)法創(chuàng)建java文件解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10SpringMVC框架整合Junit進(jìn)行單元測(cè)試(案例詳解)
本文詳細(xì)介紹在SpringMVC任何使用Junit框架。首先介紹了如何引入依賴,接著介紹了編寫(xiě)一個(gè)測(cè)試基類,并且對(duì)其中涉及的各個(gè)注解做了一個(gè)詳細(xì)說(shuō)明,感興趣的朋友跟隨小編一起看看吧2021-05-05SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡(jiǎn)化 Java 程序開(kāi)發(fā),而 SpringBoot 的出現(xiàn)是為了簡(jiǎn)化 Spring 程序開(kāi)發(fā),這篇文章主要介紹了SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02Windows下使用Graalvm將Springboot應(yīng)用編譯成exe大大提高啟動(dòng)和運(yùn)行效率(推薦)
這篇文章主要介紹了Windows下使用Graalvm將Springboot應(yīng)用編譯成exe大大提高啟動(dòng)和運(yùn)行效率,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02java實(shí)現(xiàn)外賣(mài)訂餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)外賣(mài)訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01