当前位置: 首页 > news >正文

企业项目开发--切分配置文件

此文已由作者赵计刚授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。


本章内容在第三章《Java框架整合--企业中的项目架构以及多环境分配》的代码上做修改,链接如下:

http://www.cnblogs.com/java-zhao/p/5115136.html

1、实现方式

将之前ssmm0-userManagement中类路径(src/main/resources)下的spring.xml切分成spring.xml和spring-data.xml两个文件,其中spring.xml依旧留在ssmm0-userManagement中类路径下,而spring-data.xml放到ssmm0-data的类路径下,切分后的位置如下图所示:

201812191547131a8b7b86-2979-4a0d-bb88-89a922aab36b.jpg


切分前的spring.xml如下:

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
                           
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.xxx" />
    
    <!-- 配置fastjson转换器 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <!-- 引入数据源,这里变量的读取都是从ssmm0的pom.xml中读取的 -->
    <bean id="xxxDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <!-- 引入mybatis -->
    <bean id="xxxSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="xxxDataSource" />
    </bean>
    <bean id="xxxMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 
            这里就是包名为什么就做com.xxx.mapper.user而非com.xxx.user.mapper,
            这样的话,比如说有两个项目com.xxx.mapper.user和com.xxx.mapper.hotel,value只需写作com.xxx.mapper即可
            否则,value就要写作com.xxx.user.mapper,com.xxx.hotel.mapper
         -->
        <property name="basePackage" value="com.xxx.mapper" />
        <property name="sqlSessionFactoryBeanName" value="xxxSqlSessionFactory" />
    </bean>
    
    <!-- 配置velocity -->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath">
            <value>WEB-INF/templates/</value>
        </property>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
        <property name="suffix" value=".vm" /> 
        <property name="contentType" value="text/html;charset=utf-8" />  
        <property name="dateToolAttribute" value="date"/>
        <property name="numberToolAttribute" value="number"/>
    </bean>
</beans>

切分后的spring.xml与spring-data.xml如下:

spring.xml

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
                           
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.xxx.web" /><!-- 只扫描web就可以 -->
    
    <!-- 这里需要引入ssmm0-data项目中配置的spring-data.xml(之前不引也可以成功,忘记怎么配置的了) -->
    <import resource="classpath:spring-data.xml"/>
    
    <!-- 配置fastjson转换器 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <!-- 配置velocity -->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath">
            <value>WEB-INF/templates/</value>
        </property>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
        <property name="suffix" value=".vm" /> 
        <property name="contentType" value="text/html;charset=utf-8" />  
        <property name="dateToolAttribute" value="date"/>
        <property name="numberToolAttribute" value="number"/>
    </bean>
</beans>

spring-data.xml

<?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-3.2.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd">
                           
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.xxx" />
    
    <!-- 引入数据源,这里变量的读取都是从ssmm0的pom.xml中读取的 -->
    <bean id="xxxDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <!-- 引入mybatis -->
    <bean id="xxxSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="xxxDataSource" />
    </bean>
    <bean id="xxxMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 
            这里就是包名为什么就做com.xxx.mapper.user而非com.xxx.user.mapper,
            这样的话,比如说有两个项目com.xxx.mapper.user和com.xxx.mapper.hotel,value只需写作com.xxx.mapper即可
            否则,value就要写作com.xxx.user.mapper,com.xxx.hotel.mapper
         -->
        <property name="basePackage" value="com.xxx.mapper" />
        <property name="sqlSessionFactoryBeanName" value="xxxSqlSessionFactory" />
    </bean>
    
</beans>

说明:

  • 将与controller层不直接相关的数据源与mybatis相关的配置文件分在了spring-data.xml文件中,并放置在ssmm0-data的类路径下

  • 将与controller层直接相关的fastjson转换器和velocity的配置放在了spring.xml文件中

注意:

  • spring.xml部分的注解扫描只需要扫描ssmm0-userManagement即可,而spring-data.xml处的注解扫描只需要扫描ssmm0-data中的包即可。

  • spring.xml部分需要引入spring-data.xml(但是在这之前配置的时候,并不需要引入,这一块儿有懂的朋友给哥们儿我指点一下)

2、意义

  • 将spring-data.xml放置在ssmm0-data项目中,便于我们在ssmm0-data项目中对service、dao、mapper等进行测试

  • 将来若修改数据源或者mybatis的配置,只需要修改spring-data.xml即可,而不需要修改其他每个业务模块的spring.xml,这就是将各个业务模块的spring.xml中的公共配置代码集中到一起的最大意义

  • 减少了每个业务模块中的spring.xml的重复配置代码

3、切分原则

  • 与自己模块相关的配置信息就放在自己模块下(即与ssmm0-data相关的配置就配置在ssmm0-data项目中,与ssmm-userManagement相关的配置就配置在ssmm0-userManagement中)

  • 公共的配置代码抽取出来集中在一起

注意:以第一点为重!!!

 

测试:

对于以上配置文件切分后的项目进行测试的话,要注意,先把ssmm0项目编译一下"clean compile"(见第一章),然后将ssmm0-data的项目的env改成dev(见第三章),否则使用默认的配置prod,将导致数据库连接失败,之后运行项目ssmm0-userManagement就可以了。


免费领取验证码、内容安全、短信发送、直播点播体验包及云服务器等套餐

更多网易技术、产品、运营经验分享请点击。


相关文章:
【推荐】 Docker容器的自动化监控实现
【推荐】 敲诈勒索比特币不断,企业用户如何防“山寨”钓鱼邮件

相关文章:

  • node下使用jquery
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • Analytics Zoo:在Apache Spark上实现分布式Tensorflow和BigDL管道的统一分析和AI平台
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • oracle问题 ORA-01843:not a valid month
  • 退款证书相关
  • 如何将本地文件上传至阿里云ECS中
  • React 作者关于 Hooks 的深度 issue,值得你阅读
  • 【AIX】在命令前显示完整路径
  • Promise,我是这么理解的!
  • python 怎样使用单个反斜杠\
  • 在IPv6之前
  • 数据挖掘领域经典算法——CART算法
  • JavaScript原型的实际应用
  • 微软宣布Azure Function支持Python
  • 「面试题」如何实现一个圣杯布局?
  • Akka系列(七):Actor持久化之Akka persistence
  • golang 发送GET和POST示例
  • MobX
  • mysql外键的使用
  • mysql中InnoDB引擎中页的概念
  • nodejs:开发并发布一个nodejs包
  • PAT A1092
  • windows-nginx-https-本地配置
  • 诡异!React stopPropagation失灵
  • 回顾 Swift 多平台移植进度 #2
  • 前言-如何学习区块链
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • Spring第一个helloWorld
  • #includecmath
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • $.ajax()方法详解
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (二十三)Flask之高频面试点
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (算法)前K大的和
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • ******IT公司面试题汇总+优秀技术博客汇总
  • ***原理与防范
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .net和php怎么连接,php和apache之间如何连接
  • .NET框架设计—常被忽视的C#设计技巧
  • .NET项目中存在多个web.config文件时的加载顺序
  • .net中调用windows performance记录性能信息
  • .net专家(张羿专栏)
  • // an array of int
  • @Documented注解的作用
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [C++基础]-初识模板