本章内容在第三章《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>

View Code

切分后的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>

View Code

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>

View Code

说明:

  • 将与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就可以了。

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

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

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本章内容在第三章<Java框架整合--企业中的项目架构以及多环境分配>的代码上做修改,链接如下: ...

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

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

  3. 19年8月 字母哥 第四章 常用web开发数据库框架 不要用公司网络加载不出来 用热点!!!

    第四章 常用web开发数据库框架 4.1.整合Spring JDBC操作数据 4.2 Spring JDBC多数据源的实现 4.3.Spring JDBC JTA实现分布式事务 4.4.ORM主流框架 ...

  4. 从程序员到产品经理 第四章:敏捷开发和项目管理

    目录 第四章:敏捷开发和项目管理 敏捷开发的方法和实践 Scrum和Kanban的应用

  5. PMBOK(第六版) PMP笔记——《四》第四章(项目整合管理)

    从第四章开始,进入49个过程的学习.49个过程被划分为十大知识领域,分为十个章节, 本章节是项目整合管理知识领域,主要讲述项目整合管理的7个过程. 1.需要对什么进行整合管理? 干系人需求.约束条件. ...

  6. 5、【易混淆概念集】-第四章 1 项目启动会 VS 项目开工会 变更控制的流程

    本讲主要介绍PMBOK第四章中的重要知识点,帮助你进一步理解. 本节目录 一.项目启动会 VS 项目开工会 二.变更控制的流程 一.项目启动会 VS 项目开工会 [出处]PMBOK P86,4.2.2 ...

  7. 第四章 web前端开发工程师--JavaScript京东商城项目开发 4-2 京东商城导航栏

    index.html; <!DOCTYPE html> <html lang="en"> <head><meta charset=&quo ...

  8. 第四章 web前端开发工程师--JavaScript京东商城项目开发 4-3 京东商城 源代码(整个工程)

    下载地址: 链接:https://pan.baidu.com/s/1UtjRXO_VQ97GE2GhTvesxw  提取码:m9ds      

  9. 第四章 web前端开发工程师--JavaScript京东商城项目开发 4-1 京东商城顶部图片效果

    index.html <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

最新文章

  1. 配置文件application.properties剥离
  2. python日期函数_python 时间相关函数
  3. js下拉 selenium_selenium的下拉选择框
  4. vue-router query,parmas,meta传参
  5. eclipse和mysql创建ssm_Eclipse下SSM项目的搭建
  6. C# html日期选择控件,C#实现日期选择
  7. number1(python)
  8. 计算机科学 vs 计算机技术
  9. springcloud整合Gateway
  10. 计算机无法识别3.0u盘启动,USB3.0接口不能识别U盘的解决方法
  11. java毕业设计校园社区系统mybatis+源码+调试部署+系统+数据库+lw
  12. 第三届强网杯线上赛记录
  13. Improving Entity Linking by Modeling Latent Relations between Mentions
  14. python绘制缓和曲线_autocad绘制缓和曲线
  15. Ubuntu20.04网络连接不上
  16. 让淘宝流量迅速翻倍的实用技巧
  17. vue项目集成stomp.js接收artemis消息推送
  18. delphi使用SQL的教程7
  19. 遗传算法-python版(计算机智能算法)
  20. java chunked编码解码_HTTP协议中的CHUNKED编码解析

热门文章

  1. maven error: element dependency can not have character children
  2. linux检测端口是否开放的3种命令
  3. easyui和My97DatePicker结合使用报“权限错误”的问题
  4. 隐马尔科夫模型(Hidden Markov Models) 系列之三
  5. div+css中常见的问题
  6. Thread.join的作用和原理
  7. 这里有一个让你变成技术大牛的机会
  8. SpringCloud 微服务网关Gateway 动态路由配置
  9. shell脚本手动执行成功,定时任务调用失败的解决方法。
  10. 依次从数组a中取出一个四位数,如果该四位数连续大于该四位数以后的5个数,且该数是奇数,则把这个四位数按从小到大的顺序存入数组b中,并计算满足上述条件的四位数的个数cnt。