spring boot 에서 YAML 설정 에 접근

1491 단어 JAVA

spring:
    profiles: dev | test
server:
    url: http://localhost 
    app:
        name: MyApplication
        threadCount: 4
        users: 
          - A
          - B

YAML, spring boot YAML , @ConfigurationProperties


@ConfigurationProperties("server")
@Configuration
@EnableConfigurationProperties
public class ServerProperties {
  
    private String url;
  
    private final App app = new App();
  
    public App getApp() {
        return app;
    }
    //getter and setter for url
  
    public static class App {
  
        private String name;
        private String threadCount;
        private List users = new ArrayList<>();
  
        //getters and setters
    }
     
}


@Service
public class AppService {
  
    @Autowired
    private ServerProperties config;
  
    public void printConfigs() {
        System.out.println(this.config.getUrl());
        System.out.println(this.config.getApp().getName());
        System.out.println(this.config.getApp().getThreadCount());
        System.out.println(this.config.getApp().getUsers());
    }
}

 

좋은 웹페이지 즐겨찾기