类型安全的配置文件

外部属性文件  (propeties或yml)通过@Value注解,将值注入到Java中,有可能写错,引发运行时异常等不确定的问题,可以将外部属性文件与一个Java类关联

使用

  1. @ConfigurationProperties
  1. package com.wisely.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * Created by sherry on 17/2/16.
  6. */
  7. @Component
  8. @ConfigurationProperties(prefix = "local")
  9. public class AppBean {
  10. private String name;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }
默认就会将 sources 目录下的application.yml或application.properties文件中的local带头的值注入
  1. server:
  2. port: 9090
  3. context-path: /sbapp
  4. local:
  5. name: 张柳宁AAA
要使用配置值的时候,直接使用AppBean即可
posted @ 2017-02-20 14:46  csnmd  阅读(398)  评论(0编辑  收藏  举报