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 ...
随机推荐
- “微软热爱Linux“ – 心声传遍中国
去年十月微软CEO Satya Nadella在旧金山的活动中说 “微软热爱Linux(Microsoft loves Linux)”,这句话让诸多人感到惊喜.至此之后,您可以在众多Linux和开源的 ...
- 从golang-gin-realworld-example-app项目学写httpapi (三)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/serializers.go 序列化 ...
- 修改UIView的backedlayer为CAShapeLayer
修改UIView的backedlayer为CAShapeLayer 什么叫backedlayer呢?backedlayer指的是一个View在创建好的时候就已经帮你创建好了一个与View大小一致的CA ...
- 数组list操作,切片
a=range(10) a[11:0:-1] #倒序输出 a[0:9:2] #输出2,4,6,8 a[-3:] #输出7,8,9 a.append(11) #追加一个元素 a.count(11) #统 ...
- python使用SQLAlchemy对mysql操作
安装SQLAlchemy pip install sqlalchemy 在MySQL的test数据库中创建的user表,用SQLAlchemy来试试 数据库连接 第一步,导入SQLAlchemy,并初 ...
- 43、ThreadPool 、WaitHandle、原子操作InterLocked
ThreadPool 创建线程需要时间.如果有不同的小任务要完成,就可以事先创建许多线程/在应完成这些任务时发出请求.不需要自己创建这样一个列表. 该列表由ThreadPool类托管.这个类会在需要时 ...
- python内置模块(三)
hashlib模块 通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示). Python2中使用hashlib: import hashlib m = hashlib ...
- Html5 移动应用软件开发框架 JqueryMobile SenchaTouch 介绍
一.JqueryMobile 介绍 jQuery Mobile 是 jQuery 在手机上和平板设备上的版本. jQuery Mobile 不仅会给主流移动平台带来 jQuery 核心库,而且会发布一 ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- 根据自增ID生成不重复序列号
网上看到一个例子,源地址:https://www.aliyun.com/jiaocheng/536419.html 借鉴修改一下 实现根据long类型的用户ID生成6位随机邀请码,并且根据邀请码能算出 ...