springboot import属性文件


springboot import属性文件

                                 

                                                    

***********************

示例

                           

**************

配置文件

                 

                           

                         

application.yml

name: 瓜田李下
age: 20

---
spring:
  config:
    import: classpath:/static/config/dev.yml

---
spring:
  config:
    import: file:./custom/dev.yml

                          

/static/config/dev.yml

name: 海贼王

                                  

./custom/dev.yml

server:
  port: 8000

                                   

**************

controller 层

                                   

HelloController

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @Value("${age}")
    private Integer age;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(name+"  "+age);

        return "hello";
    }
}

                                    

                                    

***********************

使用测试

                          

应用启动,控制台输出

2021-09-14 10:11:49.836  INFO 2944 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]
2021-09-14 10:11:50.034  INFO 2944 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-09-14 10:11:50.034  INFO 2944 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1474 ms
2021-09-14 10:11:50.519  INFO 2944 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8000 (http) with context path ''
2021-09-14 10:11:50.530  INFO 2944 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.63 seconds (JVM running for 3.786)

启动端口为8000,./custom/dev.yml配置生效

                              

localhost:8000/hello,控制台输出

2021-09-14 10:13:24.493  INFO 2944 --- [nio-8000-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-09-14 10:13:24.494  INFO 2944 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-09-14 10:13:24.496  INFO 2944 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
海贼王  20

/static/dev.yml覆盖了同名配置name

                              

                             

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/120280669