原生java读取properties与spring中@value、@ConfigurationProperties读取配置文件

原生java读取properties与spring中@value、@ConfigurationProperties读取配置文件

1.properties类

Properties 继承于 Hashtable。表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。

Properties 类被许多 Java 类使用。例如,在获取环境变量时它就作为 System.getProperties() 方法的返回值。

Properties 定义如下实例变量.这个变量持有一个 Properties 对象相关的默认属性列表。

简单使用

新建对象

Properties properties = new Properties();
读取到文件流

FileInputStream fileInputStream = new FileInputStream(ResourceUtils.getFile("classpath:homepage_config.properties"));
加载到properties对象中

properties.load(fileInputStream);
loda源代码

public synchronized void load(InputStream inStream) throws IOException {
    load0(new LineReader(inStream));
}
获取属性

String url = properties.getProperty("homepageconfig.api");
getProperty源代码
public String getProperty(String key) {
    Object oval = super.get(key);
 String sval = (oval instanceof String) ? (String)oval : null;
 return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

2.spring中使用@value与@ConfigurationProperties

2.1@Value注解

@Value注解的作用时通过注解将常量、配置文件中的值、其它bean的属性值注入到变量中,作为标量的初始值。

2.1.1常量注入

@Value(“#{1}”)

private int constant; //常量

@Value(“no”)

Private String no;//注入普通字符串

@Value(“classpath:com/hry/a/config.txt”)

Private Resource resourceFile //注入文件资源

@Value(“http://www/bai.du.com”)

Privatr Resource testUrl; //注入URL资源

2.1.2Bean属性、系统属性、表达式注入@Value(“#{}”)

Bean属性注入需要注入者和被注入者属于同一个IOC容器,或者父子IOC容器关系,在同一个作用域内。

@Value(“#{beanInject.another}”)

Private String fromAnotherBean //注入其它Bean属性:注入beanInject对象的属性another,类具体定义见下面

@Value(“#{systemProperties[‘os.name’]}”)

Private String systemProperetiesName; //注入操作系统属性

@Value(“#{T(java.lang.Math).random()*100.0}”)

Private double randomNumber; //注入表达式结果

//从指定属性源获取属性值(jvm属性)

@Value("#{systemProperties['spring.application.json']}")

private String systemPropertiesjson;

//从指定属性源获取属性值(系统环境属性源)

@Value("#{systemEnvironment['HOME']}")

private String systemEnvironmentHOME;

//从指定属性源获取属性值 默认值

@Value("#{systemEnvironment['HOME22']?:'default'}")

private String systemEnvironmentHOMEdefault;
2.1.3配置文件属性注入@Value(“${}”)

@Value(“#{}”)读取配置文件中的值,注入到变量中去。配置文件分为默认配置文件application.properties和自定义配置文件

application.properties。application.properties在spring boot启动时默认加载此文件

自定义属性文件。自定义属性文件通过@PropertySource加载。@PropertySource可以同时加载多个文件,也可以加载单个文件。如果相同第一个属性文件和第二个属性文件存在相同的key,则最后一个属性文件里的key启作用。加载文件的路径也可以配置变量,如下文的${anotherfile.configinject} 此值定义在第一个属性文件config.properties

第一个属性文件config.properties内容如下:

${anotherfile.configinject}作为第二个属性文件加载路径的变量值

book.name =bookName

anotherfile.configinject = placeholder

   第二个属性文件config_placeholder.properties内容如下:

          Book.name.placeholder = bookNamePlaceholder

   下面通过@Value("${app.name}")语法将属性文件的值注入bean属性值,详细代码见:
   @Component

//引用自定义配置文件

@ProperlySource({“classpath:com/hry/config.properties”,

//引用自定义配置文件。${anotherfile.configinject}则是config.propertoes文件中的第二个属性值,会被替换为config_placeholder.properties.

“classpath:com/hty/config_${anntherfile.configinject}.properties” })

Public class ConfigurationFileInjrct{

   @Value(“${app.name}”)

   Private String appName; //这里的值来自application.properties, spring boot启动时默认加载此文件

   @Value(“${book.name}”)

   Private String bookName; //注解第一个配置文件config.properties的第一个属性

   @Value(“${bookname.placeholder}”

   private String bookNamePlaceholeder; //注入第二配置的外部文件属性

}

@Value(“#{}”)和@Value("KaTeX parse error: Expected 'EOF', got '#' at position 20: …}")的区别 @Value(“#̲{}”) 表示SpEl通表达式…{xxxx}”)注解从配置文件读取值的用法

注意:

如果多个配置文件中存在相同的配置,具体来说就是两个properties文件中存在两个相同的key

2.2@ConfigurationProperties

ConfigurationProperties其实就类似于使用多个@Value同时绑定,绑定的对象就是DataSource类型的对象,而且是 隐式绑定 的,意味着在配置文件编写的时候需要与对应类的字段名称 相同。

@ConfigurationProperties 和 @value 有着相同的功能,但是 @ConfigurationProperties的写法更为方便
@ConfigurationProperties 的 POJO类的命名比较严格,因为它必须和prefix的后缀名要一致, 不然值会绑定不上, 特殊的后缀名是“driver-class-name”这种带横杠的情况,在POJO里面的命名规则是 下划线转驼峰 就可以绑定成功,所以就是 “driverClassName”。
@Value注解和@ConfigurationProperties,读取配置文件的属性
使用:

@Component
@ConfigurationProperties(prefix = "com.healai")
public class CustomConfig {

    private String chcpDatabase;
    private String ossBucketName;
    private String asqIP;
    private String env;
    private String chcpIP;
    private String heartBeatIP;
    // get、set
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值