Spring Boot: Load @Value from YAML file

前端 未结 6 1820
一整个雨季
一整个雨季 2020-12-28 14:23

I need to load a property from a .yml file, which contains the path to a folder where the application can read files from.

I\'m using the following code

6条回答
  •  庸人自扰
    2020-12-28 14:53

    In yml properties file :

    xxxx:
         page:
            rowSize: 1000
    

    Create your Yaml properties config class :

    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties(prefix = "xxxx")
    public class YmlPropertiesConfig {
    
        private Page page;
    
        public Page getPage() {
            return page;
        }
        public void setPage(Page page) {
            this.page = page;
        }
    
        public class Page {
            private Integer rowSize;
    
            public Integer getRowSize() {
                return rowSize;
            }
    
            public void setRowSize(Integer rowSize) {
                this.rowSize = rowSize;
            }
        }    
    }
    

    Finally get it and use it :

    public class XXXXController {
    
         @Autowired
         private YmlPropertiesConfig ymlProperties;
    
         public String getIt(){
    
            Integer pageRowSize = ymlProperties.getPage().getRowSize();
         }
    }
    

提交回复
热议问题