zoukankan      html  css  js  c++  java
  • SpringBoot中读取配置文件的几种方式

    1.读取application文件

    在application.yml或者properties文件中添加:

    info:
      name: xiaoming
      age: 13
      sex: 1
    
    

    读取方式如下:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /*
    * 通过@value注解的方式读取
    */
    @Component
    public class TechUser {
        @Value("${info.name}")
        private String name;
        @Value("${info.age}")
        private int age;
        @Value("${info.sex}")
        private int sex;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    }
    
    
    @ConfigurationProperties注解读取方式
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "info")
    public class TechUser {
        private String name;
        private int age;
        private int sex;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    }
    
    

    2.读取指定文件

    资源目录下建立config/db-config.properties:

    db.username=root
    
    db.password=123456
    
    
    2.1@PropertySource+@Value注解读取方式
    @Component
    @PropertySource(value = { "config/db-config.properties" })
    public class DBConfig1 {
        @Value("${db.username}")
        private String username;
        @Value("${db.password}")
        private String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    注意:@PropertySource不支持yml文件读取。

    2.2@PropertySource+@ConfigurationProperties注解读取方式
    @Component
    @ConfigurationProperties(prefix = "db")
    @PropertySource(value = { "config/db-config.properties" })
    public class DBConfig2 {
        private String username;
        private String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    3.Environment读取方式

    以上所有加载出来的配置都可以通过Environment注入获取到。

    @Autowired
    private Environment env;
    
  • 相关阅读:
    【转载】10个Web3D可视化精彩案例
    基于react的audio组件
    如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
    CSS3 用border写 空心三角箭头 (两种写法)
    浅谈微信小程序对于创业者,意味着什么?
    左手Cookie“小甜饼”,右手Web Storage
    css3中user-select的用法详解
    个人感觉一些比较有用的特效例子
    纯css模拟电子钟
    蓝桥杯 ALGO-2:最大最小公倍数
  • 原文地址:https://www.cnblogs.com/rickiyang/p/11074178.html
Copyright © 2011-2022 走看看