最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring配置文件的拆分和整合過程分析

 更新時(shí)間:2022年10月06日 10:45:25   作者:姓蔡小朋友  
在實(shí)際應(yīng)用里,隨著應(yīng)用規(guī)模的增加,系統(tǒng)中 Bean 數(shù)量也大量增加,導(dǎo)致配置文件非常龐大。為了避免這種情況的產(chǎn)生,提高配置文件的可讀性與可維護(hù)性,可以將Spring 配置文件分解成多個(gè)配置文件,感興趣的朋友跟隨小編一起看看吧

一、Spring配置文件拆分:

  • 在實(shí)際應(yīng)用里,隨著應(yīng)用規(guī)模的增加,系統(tǒng)中 Bean 數(shù)量也大量增加,導(dǎo)致配置文件非常龐大。為了避免這種情況的產(chǎn)生,提高配置文件的可讀性與可維護(hù)性,可以將Spring 配置文件分解成多個(gè)配置文件。
  • 拆分前:所有配置信息都在同一個(gè)配置文件中。

請?zhí)砑訄D片描述

<?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)建配置文件。

請?zhí)砑訄D片描述

<?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配置文件時(shí)每個(gè)ApplicationContext對象只能解析一個(gè)配置文件,所以我們需要把拆分后的所有配置文件整合后進(jìn)行統(tǒng)一解析。

請?zhí)砑訄D片描述

<?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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

新田县| 浠水县| 本溪| 禹州市| 乐业县| 团风县| 南部县| 商水县| 长汀县| 康保县| 榆林市| 芦山县| 松溪县| 台北市| 新和县| 辰溪县| 陵川县| 昭通市| 富阳市| 乐亭县| 习水县| 嘉兴市| 东乌| 六盘水市| 定结县| 石台县| 赞皇县| 莱州市| 出国| 内江市| 通城县| 高雄县| 东源县| 紫云| 临湘市| 竹北市| 锦州市| 揭阳市| 白朗县| 高雄县| 广平县|