Spring配置文件的拆分和整合過(guò)程分析
一、Spring配置文件拆分:
- 在實(shí)際應(yīng)用里,隨著應(yīng)用規(guī)模的增加,系統(tǒng)中 Bean 數(shù)量也大量增加,導(dǎo)致配置文件非常龐大。為了避免這種情況的產(chǎn)生,提高配置文件的可讀性與可維護(hù)性,可以將Spring 配置文件分解成多個(gè)配置文件。
- 拆分前:所有配置信息都在同一個(gè)配置文件中。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--添加包掃描,通過(guò)掃描包內(nèi)的注解創(chuàng)建對(duì)象--> <context:component-scan base-package="org.example.controller"></context:component-scan> <context:component-scan base-package="org.example.service"></context:component-scan> <context:component-scan base-package="org.example.dao"></context:component-scan> </beans>
按層拆分:不同層分別創(chuàng)建配置文件。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--添加包掃描,通過(guò)掃描包內(nèi)的注解創(chuàng)建對(duì)象--> <context:component-scan base-package="org.example.controller"></context:component-scan> </beans>
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--添加包掃描,通過(guò)掃描包內(nèi)的注解創(chuàng)建對(duì)象--> <context:component-scan base-package="org.example.dao"></context:component-scan> </beans>
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--添加包掃描,通過(guò)掃描包內(nèi)的注解創(chuàng)建對(duì)象--> <context:component-scan base-package="org.example.service"></context:component-scan> </beans>
二、Spring配置文件整合:
在我們解析Spring配置文件時(shí)每個(gè)ApplicationContext對(duì)象只能解析一個(gè)配置文件,所以我們需要把拆分后的所有配置文件整合后進(jìn)行統(tǒng)一解析。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--導(dǎo)入配置文件--> <!--單個(gè)導(dǎo)入--> <import resource="applicationContext_controller.xml"></import> <import resource="applicationContext_service.xml"></import> <import resource="applicationContext_dao.xml"></import> <!--批量導(dǎo)入--> <!-- 可以使用通配符進(jìn)行整合。但此時(shí)要求父配置文件名不能滿足所能匹配的格式,否則將出現(xiàn)循環(huán)遞歸包含。 就本例而言,父配置文件不能匹配 applicationContext-*.xml 的格式,即不能起名為applicationContext-total.xml。 --> <import resource="applicationContext_*.xml"></import> </beans>
到此這篇關(guān)于Spring配置文件的拆分和整合的文章就介紹到這了,更多相關(guān)Spring配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA反射機(jī)制中g(shù)etClass和class對(duì)比分析
這篇文章主要介紹了JAVA反射機(jī)制中g(shù)etClass和class對(duì)比分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Spring使用Redis限制用戶登錄失敗的次數(shù)及暫時(shí)鎖定用戶登錄權(quán)限功能
這篇文章主要介紹了Spring使用Redis限制用戶登錄失敗的次數(shù)及暫時(shí)鎖定用戶登錄權(quán)限功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作
這篇文章主要介紹了深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10在Java中FreeMarker?模板來(lái)定義字符串模板
這篇文章主要介紹了在Java中FreeMarker?模板來(lái)定義字符串模板,文章基于Java的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-04-04重寫equals的同時(shí)為何要重寫hashCode?
這篇文章主要給大家介紹了關(guān)于重寫equals的同時(shí)為何要重寫hashCode的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01