一:背景
一个项目分开发,测试,生产多个环境,一般情况下每个环境总有一些配置是不一样的,比如数据源,线程池配置等等,那我们怎么解决这个问题呢,总不能上不同环境就去改代码吧,maven就提供了一种很好的机制去解决这个问题,即maven中的profile和filter来解决这个问题。
二:案例
如我项目中要用到线程池,每个环境的线程数目和队列可能大小不一样
<bean id="taskExcutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name ="corePoolSize" value = "${threadPool.corePoolSize}" />
<property name ="keepAliveSeconds" value ="${threadPool.keepAliveSeconds}" />
<property name ="maxPoolSize" value ="${threadPool.maxPoolSize}" />
<property name ="queueCapacity" value ="${threadPool.queueCapacity}" />
</bean>
我们可以在value写上$+变量名来配置,然后将变量配在properties文件中
1.开发环境配置,文件名:config-dev.properties
2.测试环境配置,文件名:config-test.properties
3.生产环境配置,文件名:config-prod.properties