springboot读取配置文件属性

application.yml的配置

比如

 xiao.

   zg.

     host:127.0.0.1

     username :xzg85586537

那么就创建一个文件

@Data

@ConfigurationProperties(prefix="xiao.zg")   //配置文件的前缀

  public class XiaoProperties {

       String host;

       String username;

}

然后创建一个类调用配置文件的属性

@Component

@EnableConfigurationProperties(XiaoProperties .class)

   public class XiaoUtils {

     @Autowired

    private XiaoProperties  xiaoProperties ;

然后如果要用到配置文件的信息就调用xiaoProperties .getXX()

}

猜你喜欢

转载自www.cnblogs.com/zengjiao/p/10392237.html