此文已由作者赵计刚授权网易云社区发布。欢迎访问网易云社区,了解更多网易技术产品运营经验。本章内容在第三章《Java框架整合--企业中的项目架构以及多环境分配》的代码上做修改,链接如下:http://www.cnblogs.com/java-zhao/p/5115136.html1、实现方式将之前ssmm0-userManagement中类路径(src/main/resources)下的spring.xml切分成spring.xml和spring-data.xml两个文件,其中spring.xml依旧留在ssmm0-userManagement中类路径下,而spring-data.xml放到ssmm0-data的类路径下,切分后的位置如下图所示:切分前的spring.xml如下:

    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">

                           

    

    

    

    

    

        

            

        

    

    

    

    

        

        

        

        

    

    

    

    

        

    

    

        

        

        

    

    

    

    

        

            WEB-INF/templates/

        

        

            

                UTF-8

                UTF-8

            

        

    

     

         

          

        

        

    

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

    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">

                           

    

    

    

    

    

    

    

    

        

            

        

    

    

    

    

        

            WEB-INF/templates/

        

        

            

                UTF-8

                UTF-8

            

        

    

     

         

          

        

        

    

spring-data.xml

    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">

                           

    

    

    

    

    

        

        

        

        

    

    

    

    

        

    

    

        

        

        

    

    

说明:将与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容器的自动化监控实现【推荐】 敲诈勒索比特币不断,企业用户如何防“山寨”钓鱼邮件

查看原文