100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 原生java读取properties与spring中@value @ConfigurationProperties读取配置文件

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

时间:2018-10-09 18:21:03

相关推荐

原生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/”)

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}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。