Set up a profile at spring mvc multi-environment

In the actual development often you need to write some configuration information in a configuration file, such as the mysql host address, port number, user name and password. Further, the code may be used when developing a set of configuration parameters, while deploying to the test environment will use another set of configuration parameters, and then deployed to the test is completed online environment, and the need to use another set of parameters online environment . Therefore, the actual development faced with how to add a profile in multiple environments to the project, and to ensure that different environments can automatically use of different profiles.

spring provides spring.profiles.active parameters, under normal circumstances, we will set this value as a dev, test, and in the online, representing the development environment, test environment and production environment online. For example, when spring.profiles.active dev value, representing the current running in the development environment, the profile should be used in the development environment.

By passing spring.profiles.active parameters to different values ​​when running a program to select a different profile. In this way only once packaged, do not pack once for each of the different environments.

The following shows how to select a profile in the operational phase. Changes on the project on an upper (or first restore the above project to its original form).

First created in the directory resoures four profiles, respectively application.properties, application-dev.properties, application-test.properties and application-online.properties. Wherein application.properties file is stored in the configuration information are common to all environments, respectively correspond to the other three development environment, the test environment and the configuration environment online:

 

 

 

Because the configuration file will be selected to go to the operation of the project, therefore, all configuration files are required package to the project. In the first three common environment application.properties add a file configuration item, parameter city.name, is beijing:

city.name=beijing


和前面的例子一样,dev、test和online环境的配置文件都配置了一个名为env.name的参数,值分别为env-dev、env-test和env-online。

接下来修改spring.xml,增加如下一行配置如下:

1 <context:property-placeholder location="classpath:application.properties, classpath:application-${spring.profiles.active}.properties"/>


这一行是告诉spring容器去哪加载配置文件。这里添加了两个配置文件,一个是所有环境都通用的application.properties文件,另一个则取决于spring.profiles.active参数的值是多少,如果是dev(即开发环境),则是application-dev.properties文件;同理,如果是test(测试环境)则是application-test.properties文件;如果是online(线上环境)则是application-online.properties文件。

为了观察配置的效果,修改HelloServiceImpl文件的内容如下:

 1 package com.mytest.service.impl;
 2 
 3 import com.mytest.service.HelloService;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.stereotype.Service;
 6 
 7 @Service
 8 public class HelloServiceImpl implements HelloService {
 9 
10   @Value("${env.name}")
11   private String envName;
12 
13   @Value("${city.name}")
14   private String cityName;
15 
16   @Override
17   public String sayHello(String to) {
18     return "hello " + to + ", env.name:" + envName + ", cityName:" + cityName;
19   }
20 }

 

HelloServiceImpl除了引用了配置文件中的env.name参数外,还引用了通用配置文件中的city.name参数。

接下在maven命令行中执行如下命令构建整个工程:

1 clean package


这里构建时没有再传入任何参数了。

接下来将打包好的mvc-test-1.0-SNAPSHOT.war文件拷贝到tomcat的webapps目录下,并改名为mvc-test.war。

运行时进行配置文件的选择,其原理就是在用java命令执行java程序时传入给spring.profiles.active参数不同的值,比如:

1 java xxx -Dspring.profiles.active=dev


这样就在启动程序时就把spring.profiles.active参数的设置为dev了。

因为tomcat是我们通常是用它自身bin目录下的catalina.sh脚本来启动的,所以我们把启动参数写到这个脚本里。打开catalina.sh脚本,搜索到如下行:

1 JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"

将其改成:

1 JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS -Dspring.profiles.active=dev"


表示现在是开发环境。保存并退出,然后在tomcat的bin目录下执行以下命令以启动tomcat:

1 sh ./catalina.sh run


然后在浏览器中输入http://localhost:8080/mvc-test/hello?name=tom,回车即可看到结果:

 

可以看到,程序正确地选择了开发环境下的配置文件。

用同样的方法,将spring.profiles.active设置为test,并重新启动tomcat,在浏览器中访问,可以看到程序正确地选择了测试环境下的配置文件:

 

 

 

将spring.profiles.active设置为online,并重新启动tomcat,在浏览器中访问,可以看到程序正确地选择了线上环境的配置文件:

 

 

 

我们再看下tomcat的webapps目录下,mvc-test的目录结构:


可以看到所有的配置文件都打包到WEB-INF/classes目录下了,因为是运行时才选择配置文件,所以所有的配置文件都要打包过来。

综上,可以将所有环境都通用的配置信息写入到application.properties文件中,而不同运行环境下不同的配置信息则分别写入到application-dev.properties、application-test.properties和application-online.properties文件中,再通过运行程序时给sring.profiles.active参数传入不同的值(dev或test或online)即可完成不同运行环境下选择不同的配置文件的功能。

Guess you like

Origin www.cnblogs.com/haw2106/p/12084697.html