2018-03-12
springboot-配置参数
springboot 评论:0 浏览:76

转载请注明出处:https://oldnoop.tech/c/167.html

springboot加载配置方式

springboot加载配置参数,支持多达10多种方式,

(1) 命令行参数
(2)  java:comp/env 里的JNDI属性
(3) JVM系统属性
(4) 操作系统环境变量
50 第 3章 自定义配置
(5) 随机生成的带 random.* 前缀的属性(在设置其他属性时,可以引用它们,比如 ${random.
long} )
(6) 应用程序以外的application.properties或者appliaction.yml文件
(7) 打包在应用程序内的application.properties或者appliaction.yml文件
(8) 通过 @PropertySource 标注的属性源
(9) 默认属性

 

springboot加载配置参数优先级

以上列表 从上到下,优先级依次降低
这个列表按照优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先
级的相同属性。例如,命令行参数会覆盖其他属性源里的属性。


application.properties和application.yml文件能放在以下四个位置。
(1) 外置,在相对于应用程序运行目录的/config子目录里。
(2) 外置,在应用程序运行的目录里。
(3) 内置,在config包内。
(4) 内置,在Classpath根目录。

开发须知优先级关系

属性(参数)配置优先级
命令行参数

application.properties或者application.yml配置

@PropertySource引入的自定义配置

springboot获取配置参数

两种方式

通过@Value注解

通过注入Environment对象(@autowired)

通过@Value注解获取配置参数

application配置中的参数

application.yml:
-----------------------------------
config:
    appname: oldnoop application
    welcome: welcome to ${config.appname}

一般编写一个配置类,添加一些属性,然后使用@Value注解注入配置的值

 

定义一个bean获取配置的数据

-----------------------------------
@Component
public class ApplicationConfig {

    //定义私有属性,使用@Value注解注入配置的属性值
    @Value("${config.appname}")
    private String appname;
    //定义私有属性,使用@Value注解注入配置的属性值
    @Value("${config.welcome}")
    private String welcome;
    
    @Override
    public String toString() {
        return "ApplicationConfig [appname=" + appname + ", welcome=" + welcome + "]";
    }
    
}

 

测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationConfigTest {
    //引用配置bean,使用@Autowired注入
    @Autowired
    private ApplicationConfig config;

    @Test
    public void testConfigProps() {
        System.out.println(config);
    }
}

自定义属性文件的参数

通过@PropertySource引入属性文件,
优先级 比application.properties和application.yml低
如果在application中出现,会引入application中配置的值


属性文件:
src/main/resources
            |---config
                 |-----oldnoop.properties
-----------------------------------
config.appname=oldnoop prop application
config.welcome=welcome to ${config.appname}
config.locale=beijing

定义bean:
-----------------------------------
//定义一个bean获取配置的数据
@Component
@PropertySource(value = { "classpath:config/oldnoop.properties" })
public class PropertySourceConfig {

    // 定义私有属性,使用@Value注解注入配置的属性值
    @Value("${config.appname}")
    private String appname;
    
    // 定义私有属性,使用@Value注解注入配置的属性值
    @Value("${config.welcome}")
    private String welcome;
    
    @Value("${config.locale}")
    private String locale;

    @Override
    public String toString() {
        return "PropertySourceConfig [appname=" + appname + ", welcome=" + welcome + ", locale=" + locale + "]";
    }

}

测试:
-----------------------------------
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertySourceConfigTest {
    
    @Autowired
    private PropertySourceConfig config;

    @Test
    public void testPropertySourceProps() {
        System.out.println(config);
    }
}

 

通过Environment方式获取

注入Environment属性,调用getProperty方法获得参数

@RunWith(SpringRunner.class)
@SpringBootTest
//使用自定义的属性文件
@PropertySource(value = { "classpath:config/oldnoop.properties" })
public class EnvironmentTest {

    @Autowired
    private Environment env;
    
    @Test
    public void test(){
        System.out.println(env.getProperty("config.appname"));
        System.out.println(env.getProperty("config.welcome"));
        System.out.println(env.getProperty("config.locale"));
    }
}

 

 



  • 转载请注明出处:https://oldnoop.tech/c/167.html

Copyright © 2018 oldnoop.tech. All Rights Reserved

鄂ICP备2023022735号-1