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

 更新时间:2022年10月06日 10:45:25   作者:姓蔡小朋友  
在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件非常庞大。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将Spring 配置文件分解成多个配置文件,感兴趣的朋友跟随小编一起看看吧

一、Spring配置文件拆分:

  • 在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件非常庞大。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将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">
    <!--添加包扫描,通过扫描包内的注解创建对象-->
    <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>

按层拆分:不同层分别创建配置文件。

请添加图片描述

<?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">
    <!--添加包扫描,通过扫描包内的注解创建对象-->
    <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">
    <!--添加包扫描,通过扫描包内的注解创建对象-->
    <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">
    <!--添加包扫描,通过扫描包内的注解创建对象-->
    <context:component-scan base-package="org.example.service"></context:component-scan>
</beans>

二、Spring配置文件整合:

在我们解析Spring配置文件时每个ApplicationContext对象只能解析一个配置文件,所以我们需要把拆分后的所有配置文件整合后进行统一解析。

请添加图片描述

<?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>
    <!--批量导入-->
    <!--
      可以使用通配符进行整合。但此时要求父配置文件名不能满足所能匹配的格式,否则将出现循环递归包含。
      就本例而言,父配置文件不能匹配 applicationContext-*.xml 的格式,即不能起名为applicationContext-total.xml。
    -->
    <import resource="applicationContext_*.xml"></import>
</beans>

到此这篇关于Spring配置文件的拆分和整合的文章就介绍到这了,更多相关Spring配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaWeb 网上书店 注册和登陆功能案例详解

    JavaWeb 网上书店 注册和登陆功能案例详解

    这篇文章主要介绍了JavaWeb 网上书店 注册和登陆功能,结合具体案例形式详细分析了JavaWeb 网上书店 注册和登陆功能具体实现步骤、操作技巧与注意事项,需要的朋友可以参考下
    2019-08-08
  • 在netty中使用native传输协议的方法

    在netty中使用native传输协议的方法

    这篇文章主要介绍了在netty中使用native传输协议,这里我们只以Kqueue为例介绍了netty中native传输协议的使用,需要的朋友可以参考下
    2022-05-05
  • 解决springboot 部署到 weblogic 中 jar 包冲突的问题

    解决springboot 部署到 weblogic 中 jar 包冲突的问题

    这篇文章主要介绍了springboot 部署到 weblogic 中 jar 包冲突,weblogic 也有是解决方案的,可以通过新增并配置 weblogic.xml 文件来定义哪些类需要优先从项目工程包的 jar 包中加载,本文给大家分享解决方法,需要的朋友可以参考下
    2022-08-08
  • Java 服务端消息推送的实现小结

    Java 服务端消息推送的实现小结

    本文主要介绍了Java 服务端消息推送的实现小结,主要包括四种常见的消息实时推送方案:短轮询、长轮询、SSE 和 WebSocket,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • java获取和设置系统变量问题(环境变量)

    java获取和设置系统变量问题(环境变量)

    这篇文章主要介绍了java获取和设置系统变量问题(环境变量),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • java与c#的语法区别详细介绍

    java与c#的语法区别详细介绍

    由C#转入Java一段时间了,总结下个人认为的Java同C#语法之间的不同之处,有不同意见之处还望各位海涵,刚学Java时觉得语法同C#大致是相同的(应该说C#同Java大致相同
    2012-11-11
  • Java类初始化和实例化中的2个“雷区”

    Java类初始化和实例化中的2个“雷区”

    这篇文章主要介绍了Java类初始化和实例化中的2个“雷区”,大家要注意,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • 深入理解Mybatis一级缓存

    深入理解Mybatis一级缓存

    客户端向数据库服务器发送同样的sql查询语句,如果每次都去访问数据库,会导致性能的降低,那么怎么提高呢?下面小编给大家分享下mybatis为我们提供了一级缓存的策略
    2016-12-12
  • MyBatis执行Sql的流程实例解析

    MyBatis执行Sql的流程实例解析

    这篇文章主要介绍了MyBatis执行Sql的流程实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Struts2实现CRUD(增 删 改 查)功能实例代码

    Struts2实现CRUD(增 删 改 查)功能实例代码

    CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影。接下来通过本文给大家介绍Struts2实现CRUD(增 删 改 查)功能实例代码,感兴趣的朋友一起看看吧
    2016-06-06

最新评论