针对不同的环境我们希望有不同的配置,在springboot中如何配置呢?
可以这样写application.yaml:
spring: profiles: active: dev --- server: addresss: default --- spring: profiles: prod server: addresss: 1.1.1.1 --- spring: profiles: dev server: addresss: 127.0.0.1
然后代码这样写:
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; import org.springframework.boot.context.properties.ConfigurationProperties; @RestController @EnableAutoConfiguration @ConfigurationProperties("server") public class MyMain { private String addresss; public void setAddresss(String a) { this.addresss = a; } public String getAddresss() { return addresss; } @RequestMapping("/") String home() { return "Hello world!~~Now active yaml:" + addresss; } public static void main(String[] args) { SpringApplication.run(MyMain.class, args); } }
最终执行的结果就是:
原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2060