Maven多module下的Spring启动

本篇blog属于个人工作时的经验之谈,在使用Maven进行项目构建Spring项目中遇到的启动问题,纯属个人见解。

项目结构

在实际开发中,往往是用Maven来进行构建项目,一个项目一般都由多个Maven module组成,例如:

  • xxx-base(项目启动module,以下为当前module依赖的module)
    • xxx-common
    • xxx-service(spring配置文件,属性properties文件)
    • ...

xxx-service的spring配置文件:




    
        
            classpath*:properties/xxx.properties
        list>
    

xxx-service的properties文件:

redis.host=${redis.host}
redis.port=${redis.port}
redis.password=${redis.password}

redis.timeout=100000
redis.pool.maxIdle=600
redis.pool.maxTotal=1500

项目启动

xxx-base的spring配置文件:

  1. 报错方式配置



    
        
            classpath:properties/xxx.properties
        
    



异常如下图:

错误异常
  1. 解决方案

在存在PropertyPPlaceholderConfigure Bean的每个配置文件中都加上,如下所示:


      
    
        
            classpath:properties/xxx.properties
        
    

  1. 自动扫描spring配置文件

当一个工程引用的module过多时,那么需要import的spring配置文件就特别多,这时可以将每个module的配置文件都使用相同的命名,例如applicationContext.xml

在项目启动时通过如下代码启动:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");

这样就可以去除掉一大串的import标签了。

你可能感兴趣的:(Maven多module下的Spring启动)