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