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

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

本章内容在第三章《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的类路径下,切分后的位置如下图所示:

切分前的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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://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.xsdhttp://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就可以了。

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

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

相关文章:
【推荐】 jq 一个强悍的json格式化查看工具
【推荐】 Kubernetes网络方案的三大类别和六个场景
【推荐】 深情留不住,套路得人心--聊聊套路那些事儿

转载于:https://www.cnblogs.com/zyfd/p/10150741.html

企业项目开发--切分配置文件相关推荐

  1. 第四章 企业项目开发--切分配置文件

    本章内容在第三章<Java框架整合--企业中的项目架构以及多环境分配>的代码上做修改,链接如下: http://www.cnblogs.com/java-zhao/p/5115136.ht ...

  2. 企业项目开发--分布式缓存memcached(3)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 3.3.ssmm0-data 结构: 3.3.1.pom.xml 1 <?xml version=&q ...

  3. 企业项目开发--企业中的项目架构以及多环境分配(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 2.2.2.ssmm0-data pom.xml <?xml version="1.0&qu ...

  4. 企业项目开发中的问题

    1.用sql语句嵌入到代码中呢,还是全部用存储过程,放在一起好管理.   sql语句和存储过程如何选择呢,是不是简单的就直接用sql语句,复杂一点的用sp 2.uml图用什么软件画比较好一点呢.stu ...

  5. 企业项目开发中可遵循的时间管理守则-华为时间管理

  6. ASP.NET3.5 企业级项目开发 -- 第二章(续) 数据访问层(DAL)的开发解决方案提出...

    ASP.NET3.5 企业级项目开发 -- 第二章(续) 数据访问层(DAL)的开发解决方案提出 前言:首先给大家说声"对不起",因为自从打算写这系列的文章以来,得到大家很多的支持 ...

  7. ASP.NET3.5 企业级项目开发 -- 第二章 数据访问层(DAL)的开发

    为什么80%的码农都做不了架构师?>>>    ASP.NET3.5 企业级项目开发 -- 第二章 数据访问层(DAL)的开发          前言:本篇主要讲述数据访问层的开发, ...

  8. 轻松玩转Makefile | 企业项目级Makefile实例

    前言 本文展示了一个比较完整的企业项目级别的Makefile文件,包括了:文件调用,源文件.头文件.库文件指定,软件版本号.宏定义,编译时间,自动目录等内容. 1.目录架构 本文中所采用的目录架构,在 ...

  9. 缺项目经验?线上实训机会来咯!!后端企业项目盖章实习,可写进简历

    软件行业近十余年得到了飞速的发展,互联网行业的高薪也不断吸引着优秀的人才选择进入软件开发岗位,即便近期整体招聘行情遇冷,但数字化的国家战略不会轻易改变,人才供给速度也仍未赶上技术发展的速度,因此软件开 ...

最新文章

  1. 设计模式:单例模式之枚举
  2. 在有赞工作两年半的感受
  3. c++学习笔记之异常
  4. 使用echarts时,鼠标首次移入屏幕会闪动,屏幕会出现滚动条
  5. 征稿 | “健康知识图谱”投稿通道开启
  6. xshell xftp 工具免费版本免费下载
  7. kinect体感绿幕抠像,AR虚拟互动拍照,体感抠像拍照
  8. 二维码设备巡检解决方案
  9. 「2019.7.22 考试」AC和WA0一步之遥
  10. 数码显示实验报告C语言,数码管动态显示实验报告
  11. 停车场车辆计数案例---以西门子1200PLC演示
  12. 机器学习预测结果评估展示_评估通用社区测试计划的性能并预测结果
  13. 以太坊客户端mist和geth加快区块同步速度的方法
  14. linux ps 简书,史上最全ps 命令解析
  15. python带你制作随机点名系统,超级简单
  16. 数组名 int a[5] = {1,2,3,4,5}; int *ptr = (int *)( a + 1);
  17. 使用校园网,下载知网资料
  18. 新书推荐 |《Redis 5设计与源码分析》
  19. java socket编程心跳_Java Socket编程心跳包创建实例解析
  20. 墨尔本大学 计算机科学,墨尔本大学计算机科学硕士专业详解 成为IT大神的必经之路...

热门文章

  1. 删除mongodb库
  2. php 获取js变量
  3. hdu5246 超级赛亚ACMer (百度之星初赛)(模拟)
  4. 45种Javascript技巧大全
  5. 对Erlang开发者的几点建议
  6. run-time cloud server system development recode
  7. 自己动手制作笔记本SP2系统安装光盘
  8. hive in 写法/linux OR CDH如果查看hive的版本
  9. SpringBoot—— @ComponentScan
  10. UVA-10714 Ants---蚂蚁模拟