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

目录
  • 一、Spring配置文件拆分:
  • 二、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配置文件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代码详解

    java代码 package com.oauth.util; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentat

  • spring+hibernate 两种整合方式配置文件的方法

    之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因为工作的需要,最近在使用hibernate 所以下面我们来看看 spring整合hibernate的配置文件,这里只说spring+hibernate 的配置文件而不说springmvc 因为这些是不用变的. spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 1.注解方式实现: applicationContext.xml配置

  • spring与mybatis整合配置文件

    最近因为项目要求整合了spring+mybatis架构进行项目开发,现将相关整合配置文件整理如下: 基本架构:spring+springmvc+mybatis 分布式框架:dubbo+zookeeper 数据库:mysql 数据库连接池:Druid 1 数据库连接配置信息jdbc.properties #mysql version database druid # setting validationQuery=SELECT 1 jdbc.driverClassName=com.mysql.jd

  • springboot的yml配置文件通过db2的方式整合mysql的教程

    springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件 先配置MySQL,代码如下 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: # 主库数据源 master: url: jdbc:mysql://localhost:3308/<数据库名>?useUnicode=true&characterEncoding

  • Spring 整合多个配置文件的方法

    对于一个大型应用来讲,可能存在多个配置文件.我们可以在启动 Spring 容器时,通过 String 数组来指定这些配置文件 . Spring 还可以通过 <import> 将多个配置文件引入到一个文件中,集成这些配置文件,这样在启动 Spring 容器时,仅需要指定这个主配置文件即可 . 比如以下的这个主配置文件: <import resource="classpath:beans2.xml"/> <bean id="author"

  • Spring整合SpringMVC + Mybatis基础框架的配置文件详解

    前言 新建一个普通的Maven项目 基本目录结构 ├── src # │ ├── main # │ │ └── java # java代码目录 │ │ └── resources # 配置文件目录, 存放下面Spring配置文件 │ ├── test # 单元测试目录 ├── web # web目录 │ └── WEB-INF # web.xml 配置文件目录 1. Mybatis层编写 1.在 resources 目录下新建数据库配置文件 database.properties jdbc.dr

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

    目录 一.Spring配置文件拆分: 二.Spring配置文件整合: 一.Spring配置文件拆分: 在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件非常庞大.为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将Spring 配置文件分解成多个配置文件. 拆分前:所有配置信息都在同一个配置文件中. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu

  • spring与mybatis三种整合方法

    1.采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean. spring-mybatis.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3

  • Spring与Mybatis基于注解整合Redis的方法

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载后直接启动redis-server就行了,见下图: 一,先上jar包 二,创建实体类 package com.sl.user.vo; import java.io.Serializable; import com.fasterxml.jackson.databind.PropertyNamingSt

  • 关于Spring配置文件加载方式变化引发的异常详解

    目录 问题背景 过程 定位 根因 配置加载顺序 解决 问题背景 我们项目的配置文件一直是通过Apollo进行管理,但是近期由于某些特殊的部署需求,需要使用K8S的原生对象来获取配置,如此一来的话,就需要使用环境变量spring.config.location来指定application.properties文件的路径,以便动态的获取配置. 说明:项目是一个dubbo项目,配置文件中主要包括一些基础组件的配置.以及dubbo相关的配置. 这时候问题来了,在所有配置及代码都没有变化的情况下,如果不指

  • Spring Boot 2.6.x整合Swagger启动失败报错问题的完美解决办法

    目录 问题 原因 解决方案 方案一(治标) 方案二(治本) 总结 问题 Spring Boot 2.6.x版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动容器会报错: Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception… 原因 Springfox 假设 Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Bo

  • Spring Boot 利用 XML 方式整合 MyBatis

    目录 一.前言 二.整合过程 新建 Spring Boot 项目 添加 pom 依赖 准备数据库 pojo 层 dao 层 service 层 controller 层 入口程序配置 网页测试 总结 一.前言 上一篇文章中我们已经Spring Boot 利用注解方式整合 MyBatis,今天我们就来看看,如何利用 XML 文件的方式来将两者整合起来! 下图是整个整合过程,接下来开始整合: 二.整合过程 最终项目结构如下图所示: 新建 Spring Boot 项目 新建一个 Spring Boot

  • Spring Boot 利用注解方式整合 MyBatis

    目录 前言 整合过程 新建 Spring Boot 项目 添加 pom 依赖 准备数据库 pojo 层 dao 层 service 层 controller 层 入口程序配置 网页测试 总结 前言 目前而言,国内大家使用最多的持久层框架可能还是 MyBatis 吧,那既然如此,更强大的 Spring Boot 遇上炽手可热的 MyBatis,又会擦出什么样的火花呢? 那本文就来看看,如何利用 SpringBoot 来整合 Mybatis. 如下图是总结的整合过程的大概流程,那接下来我们就来开始具

  • JSP Spring配置文件中传值的实例详解

    JSP Spring配置文件中传值的实例详解 通过spring提供方法,在配置文件中取传值 调用get方法  targetObject :指定调用的对象       propertyPath:指定调用那个getter方法 例1: public class Test1 { private String name = "nihao"; public String getName() { return name; } } Xml代码 <bean id="t1" cl

  • 监听器获取Spring配置文件的方法

    我们在做项目的时候,会用到监听器去获取Spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出来放到application里,即在监听器里调用后台商品业务逻辑的方法,也就是说我们需要在监听器里获取Spring中配置的相应的bean.先把监听器创建出来: 1. 创建InitDataListener 创建一个监听器InitDataListener继承ServletContextListener:

  • Spring 配置文件XML头部文件模板实例详解

    普通spring配置文件模板: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.s

随机推荐