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;
   }
}

为什么读取不到值?

flute
浏览 8675回答 2
2回答

靈寶

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

乌云下的风

你试试把文件名改成  application-dubbo.yml, 我认为是springboot 的命名规范引起的
打开App,查看更多内容
随时随地看视频慕课网APP