您的位置:首页 > 编程语言 > Java开发

通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换

2017-12-14 15:25 1006 查看
1、在resources目录下创建applicationContext-profile.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-4.1.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-lazy-init="true">

<description>Spring公共配置 </description>

<beans profile="production">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:common/*.properties, classpath*:production/*.properties" />
</beans>

<beans profile="test">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:common/*.properties, classpath*:test/*.properties" />
</beans>

<beans profile="dev">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:common/*.properties, classpath*:dev/*.properties" />
</beans>

</beans>

2、在resources目录下创建common【公共文件】、production【正式环境】、test【测试环境】、dev【开发环境】等目录,并存放相应环境的properties文件

3、在spring-mvc.xml文件加载上面创建的applicationContext-profile.xml文件:

<import
resource="classpath*:/applicationContext-profile.xml"/>

4、在pom.xml文件添加默认的环境

<profiles>

    <profile>
<id>dev</id>
<activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <properties>  

                <profiles.activation>dev</profiles.activation>  

            </properties>
</profile>

   

    </profiles>

5、tomcat服务器的catalina.sh文件启动参数设置:

#正式:
JAVA_OPTS="-Dspring.profiles.active=production"

#开发:
JAVA_OPTS="-Dspring.profiles.active=dev "

#测试:
JAVA_OPTS="-Dspring.profiles.active=test"

参考资料:
http://sishuok.com/forum/blogPost/list/7936.html http://www.blogjava.net/paulwong/archive/2014/03/28/411596.html https://www.cnblogs.com/strugglion/p/7091021.html https://www.cnblogs.com/yilunzhang/p/7838708.html http://blog.csdn.net/wantken/article/details/31764609
转自:http://blog.csdn.net/thinkfly123thinkfly/article/details/51094083
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐