快捷导航

SpringBoot 2.X 读取自定义yml配置文件

此处resources文件目录下有一个自定义yml文件,里面自定义了一些配置信息:

dubbo:  accepts: 700  appName: dsc-core  connections: 2  monAddress: ''  port: 57777  protocol: dubbo  resAddress: zookeeper://10.10.10.30:2181?backup=10.10.10.40:2181,10.10.10.45:2181  resPassowrd: ''  resUsername: ''然后新建DubboConfig来读取配置信息:
@Component@PropertySource(value = "classpath:dubbo.yml",encoding = "utf-8")@ConfigurationProperties(prefix = "dubbo")public class DubboConfig {   private Integer accepts;   private String appName;   private Integer connections;   private String monAddress;   private Integer port;   private String protocol;   private String resAddress;// zookeeper://10.10.10.30:2181?backup=10.10.10.40:2181,10.10.10.45:2181   private String resPassowrd;// ''   private String resUsername;// ''   @Override   public String toString() {      return "DubboConfig{" +            "accepts=" + accepts +            ", appName='" + appName + '\'' +            ", connections=" + connections +            ", monAddress='" + monAddress + '\'' +            ", port=" + port +            ", protocol='" + protocol + '\'' +            ", resAddress='" + resAddress + '\'' +            ", resPassowrd='" + resPassowrd + '\'' +            ", resUsername='" + resUsername + '\'' +            '}';   }   public Integer getAccepts() {      return accepts;   }   public void setAccepts(Integer accepts) {      this.accepts = accepts;   }   public String getAppName() {      return appName;   }   public void setAppName(String appName) {      this.appName = appName;   }   public Integer getConnections() {      return connections;   }   public void setConnections(Integer connections) {      this.connections = connections;   }   public String getMonAddress() {      return monAddress;   }   public void setMonAddress(String monAddress) {      this.monAddress = monAddress;   }   public Integer getPort() {      return port;   }   public void setPort(Integer port) {      this.port = port;   }   public String getProtocol() {      return protocol;   }   public void setProtocol(String protocol) {      this.protocol = protocol;   }   public String getResAddress() {      return resAddress;   }   public void setResAddress(String resAddress) {      this.resAddress = resAddress;   }   public String getResPassowrd() {      return resPassowrd;   }   public void setResPassowrd(String resPassowrd) {      this.resPassowrd = resPassowrd;   }   public String getResUsername() {      return resUsername;   }   public void setResUsername(String resUsername) {      this.resUsername = resUsername;   }}为什么读取不到值?

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员2

springboot 只支持application-xxx.yml的形式的配置文件。在application.yml中include一下就可以了。
回复

使用道具 举报

你试试把文件名改成 application-dubbo.yml, 我认为是springboot 的命名规范引起的
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP