1 多环境配置文件
在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置,具体步骤如下: 目录结构
dev
#开发环境配置文件
server.port=8080
server.servlet.context-path=/dev
product
#生产环境配置文件
server.port=8083
server.servlet.context-path=/product
ready
#准生产环境配置文件
server.port=8082
server.servlet.context-path=/ready
test
#测试环境配置文件
server.port=8081
server.servlet.context-path=/test
核心配置文件
#springboot核心配置文件
spring.profiles.active=test
控制器类
import com.abc.springboot.web.SpringBootController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
*/
@Controller
public class IndexController {
@RequestMapping(value = "/say")
@ResponseBody
public String say() {
return "Hello springboot multi-environments";
}
}
入口类
import com.abc.springboot
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
结果截图
spring.profiles.active=dev
spring.profiles.active=test
2.自定义配置文件
在 SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,然后采用如下注解去读取配置的属性值
2.1 @Value
核心配置文件
在核心配置文件 applicatin.properties 中,添加两个自定义配置项 school.name 和
website。在 IDEA 中可以看到这两个属性不能被 SpringBoot 识别,背景是桔色的
在核心配置文件 applicatin.yml 中,添加两个自定义配置项 school.name 和website。
server:
port: 9090
servlet:
context-path: /
school:
name: ssm
websit: http://www.baidu.com
测试代码
package com.liuhaiyang.springboot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class OneController {
@Value("${schools.name}")
public String schoolName;
@Value("${websit}")
private String websit;
@RequestMapping("one")
@ResponseBody
public String onetest(){
return "hello"+schoolName+": "+websit;
}
}
2.2 @ConfigurationProperties
自定义配置文件
#设置内嵌Tomcat端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/
first.name=tencent
first.website=https://www.tencent.com
second.name=baidu
second.website=https://www.baidu.com
自定义一个类,获取自定义配置文件中的属性值
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*
*/
@Component
@ConfigurationProperties(prefix = "first") //@ConfigurationProperties(prefix = "first") 这个注解,相当于获取到属性的前缀名,first.name、first.website。
public class First {
private String name;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
控制器类
import com.songzihao.springboot.config.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
*/
@Controller
public class IndexController {
@Autowired
private First first;
@RequestMapping(value = "/say")
@ResponseBody
public String say() {
return "first.name===" + first.getName() + " , first.website===" + first.getWebsite();
}
}
入口类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
结果截图