作者:似风似戏是梦而已 | 来源:互联网 | 2023-05-17 09:57
1.springboot的核心配置springboot项目建立之后,已经创建好了application.properties配置文件其实,配置文件还支持*.yml格式的;
1. spring boot 的核心配置
spring boot 项目建立之后,已经创建好了application.properties 配置文件
其实, 配置文件还支持*.yml 格式的;
2. 多配置环境的配置文件(实际开发)
application-dev.properties
application-test.properties
application-online.properties
多环境的配置文件, 这时候我们需要在application.properties配置一下, 激活其中某个配置文件, 具体配置如下:
spring.profiles.active=dev (开发环境)
那么问题来了, 如果开发环境中配置端口8089, application.properties中也配置了8080, 最终是生效开发环境8089的端口
3. spring boot的自定义配置文件
在application.properties 里面配置好你想要的配置, 之后在cotnroller中使用@value注解获取自定配置的值
然后在controller中获取
另外一种读取自定义配置的方法
定义一个类, 然后读取值到这个类的属性中, 之后调用类的属性获取配置
/**
* 定义自定属性中的值
*/
@Component
@ConfigurationProperties(prefix = "boot")
public class ConfigInfo {
private String country;
private int age;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@Controller
public class ConfigController {
@Value("${boot.country}")
private String country;
@RequestMapping("/boot/config")
public @ResponseBody String config(){
return country;
}
@Autowired
private ConfigInfo configInfo;
@RequestMapping("/boot/config2")
public @ResponseBody String config2(){
return configInfo.getCountry() + configInfo.getAge();
}
}