java 加载并读取Properties 文件
1 .系统自带的application.properties (以下代码仅供参考,不能粘贴复制)
假设application.properties文件有下面两个值:
come.test.name = chen
come.test.age = 18
come.test.phone = 18795812345
直接在类中的成员变量上添加注解@Value("${ }"),如下
public class Test(){
@Value("${come.test.name}")
public String name;
@Value("${come.test.age}")
public String age;
@Value("${come.test.phone}")
public String phone;
public static String getProperties(){
String value = "name" + name + "age" + age + "phone" + phone;
return value;
}
}
注意:上面的类Test已经读取加载properties文件,如果你还想在别的类中引用这个Test类。一定在Test类上面加上@Component
@Component
public class Test(){
@Value("${come.test.name}")
public String name;
@Value("${come.test.age}")
public String age;
@Value("${come.test.phone}")
public String phone;
public static String getProperties(){
String value = "name" + name + "age" + age + "phone" + phone;
return value;
}
}
Controller层的类引用Test;
@RestController
public class Demo{
@Autowired
Test test;
public String getPro(){
return test.getProperties();
}
}
2 . 如果你是自定义的properties文件。只需要在Test类加上注解 @PropertySource("classpath:xxx.properties")
@Component
@PropertySource("classpath:xxx.properties")
public class Test(){
@Value("${come.test.name}")
public String name;
@Value("${come.test.age}")
public String age;
@Value("${come.test.phone}")
public String phone;
public static String getProperties(){
String value = "name" + name + "age" + age + "phone" + phone;
return value;
}
}
java 加载并读取Properties 文件的更多相关文章
- 1. Spring基于xml加载和读取properties文件配置
在src目录下,新建test.properties配置文件,内容如下 name=root password=123456 logArchiveCron=0/5 * * * * ? 一种是使用sprin ...
- Java使用ResourceBundle类读取properties文件中文乱码的解决方案
Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...
- xml 加载多个properties文件
xml 配置项: <bean id="propertyConfigurer" class="com.boc.icms.archive.util.ExProperty ...
- Java 加载动态库 dll 文件
不知道具体原理,但是,加载 dll 文件时,带路径或者更改 dll 文件的名字,都会报错.虽然库记载成功了,但是处女座认为这不可接受.于是有了这个解决方案. 在根目录为库创建软连接,然后使用 syst ...
- maven 不同环境加载不同的properties 文件
http://haohaoxuexi.iteye.com/blog/1900568 //参考文章 实际项目中pom配置如下 <profiles> <profile> <i ...
- 分别用Java和JS读取Properties文件内容
项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...
- Java实现动态加载读取properties文件
问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- JAVA加载Properties配置资源文件
JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...
随机推荐
- c# 设计模式 之:工厂模式之---简单工厂
1.uml类图如下: 具体实现和依赖关系: 实现:SportCar.JeepCar.HatchbackCar 实现 Icar接口 依赖: Factory依赖 SportCar.JeepCar.Hatc ...
- Oracle常用名词解释
好久没做rac,最近要做架构梳理,这里针对Oracle常用的名词缩写,这里做个记录,希望对大家有所帮助. RAC 全称是Real Application Cluster,oracle的高可用群集,即实 ...
- 如何写自定义的AlertView
如何写自定义的AlertView 效果 说明 考虑到后面的可定制性以及可维护性,本人用AbstractAlertView定义了AlertView抽象的基类,实现通过子类来完成. 注:这只是粗略的写了一 ...
- [翻译] EAColourfulProgressView
EAColourfulProgressView 效果: EAColourfulProgressView is a custom progress view where the current fill ...
- HBase编程 API入门系列之create(管理端而言)(8)
大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...
- July 31st 2017 Week 31st Monday
Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperace would be ...
- December 25th 2016 Week 53rd Sunday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. What can we do if there is no fruit of ...
- 彻底关闭Firefox自动更新的方法
自己最近在使用firefox进行自动化测试时,发现配置好的firefox总是会自动更新,主要是因为一些高版本的浏览器无法安装firepath,没有firepath的火狐浏览器使用起来总是有很多的不方便 ...
- 015.2Condiction接口
Condiction对象能够让线程等待,也能够唤醒相应的线程,通过下面方法,具体看代码:await();signal();signalAll(); 使用步骤:1)创建锁2)通过锁拿到Condictio ...
- charles license key
Download: http://www.charlesproxy.com/ (Official Web-site) Registered name: anthony ortolani License ...