在平时开发时,会发现公司的项目会不同的环境使用到不同的配置。如本地,测试,预发布,发布等环境,像数据库这些都要使用到不同的配置。如果只有一个配置文件,发布到不同环境的时候都要去修改一遍那简直就是遭罪,那么,如何实现SpringBoot根据需要去加载不同的配置文件?
项目构建 SpringBoot提供简单配置能够让我们进行不同配置文件的加载。
创建Maven项目 这里简单的常见一个用户类和控制器模仿一般的逻辑,获取用户的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > </dependency > <dependency > <groupId > com.google.code.gson</groupId > <artifactId > gson</artifactId > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.68</version > </dependency > </dependencies >
用户类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class User { private String username; private String address; private String email; private String id; private String message; public User (String id) { this .username = "张三" ; this .address = "亚历山大" ; this .email = "sing@example.com" ; this .id = id; } }
控制器
1 2 3 4 5 6 7 8 9 10 11 12 @RestController public class Controller { @Value("${custom.value}") private String customValue; @RequestMapping(value = "/userInfo/{id}",method = RequestMethod.GET) public JSON getUserInfo (@PathVariable("id") String id) { User user = new User(id); user.setMessage(customValue); return JSONObject.parseObject(JSON.toJSONString(user)); } }
启动器
1 2 3 4 5 6 @SpringBootApplication public class Application { public static void main (String[] args) { SpringApplication.run(Application.class,args); } }
说明:这里利用Restful风格的请求方式,获取用户的个人信息,从配置文件中获取message复制给用户,然后以JSON的形式返回给页面。
这里来看配置配置文件
application.yml
1 2 3 4 5 server: port: 8000 custom: value: This Is Application
利用postman请求发现没有问题,注意看属性message的值 。
进行多文件配置 application-dev.yml
1 2 3 4 5 server: port: 8001 custom: value: This Is Application-dev
application-uat.yml
1 2 3 4 5 server: port: 8002 custom: value: This Is Application-uat
application-pro.yml
1 2 3 4 5 server: port: 8003 custom: value: This Is Application-pro
如果要启动使用不同的配置配置文件name就需要在application.yml
中添加额外的配置
1 2 3 spring: profiles: active: uat
这样在启动的时候就使用的是uat的配置了,启动查看message信息。
我们可以看到message的信息就是application-uat.yml
里面配置的,同时还能注意到当我们application.yml
和application-uat.yml
进行相同的配置是,SpringBoot会优先使用指定配置profiles的值。如果没有才会去application.yml
里面找。
补充 Maven打包 那么说到了SpringBoot加载不同的配置文件启动,那么不妨说一下Maven根据不同环境用不同的配置文件打包。
在pom.xml中添加配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <profiles > <profile > <id > dev</id > <properties > <env > dev</env > </properties > </profile > <profile > <id > uat</id > <properties > <env > uat</env > </properties > <activation > <activeByDefault > true</activeByDefault > </activation > </profile > <profile > <id > pro</id > <properties > <env > pro</env > </properties > </profile > </profiles > <!--build标签中添加如下的配置> <resources > <resource > <directory > src/main/resources</directory > <filtering > true</filtering > <excludes > <exclude > application.yml</exclude > <exclude > application-dev.yml</exclude > <exclude > application-pro.yml</exclude > <exclude > application-uat.yml</exclude > </excludes > </resource > <resource > <directory > src/main/resources</directory > <filtering > true</filtering > <includes > <include > application-${env}.yml</include > <include > application.yml</include > </includes > </resource > </resources >
配置完成后依次执行命令mvn clean ; mvn package
进行打包,maven会根据你的配置去打包相应的文件,打包完成后如图包含的配置文件。
使用postman去请求得到的结果,如图: