企业项目开发--切分配置文件
此文已由作者趙計剛授權網易云社區發布。
歡迎訪問網易云社區,了解更多網易技術產品運營經驗。
本章內容在第三章《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
總結
以上是生活随笔為你收集整理的企业项目开发--切分配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Node版本管理nvm, npm
- 下一篇: 自定义组件 点击空白处隐藏