public class Main {

     public static void main(String[] args) throws IOException {
//创建Properties对象
Properties prop = new Properties();
//读取classPath中的properties文件
prop.load(Main.class.getClassLoader().getResourceAsStream("bean.properties"));
//根据键取出值
String className = prop.getProperty("className");
System.out.println(className); }
}
  输出className对应的值

封装的工具类

 public class PropertyUtil {

     private static Properties prop = new Properties();

     static {
try {
prop.load(PropertyUtil.class.getClassLoader().getResourceAsStream("calculator.properties"));
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
} /**
* 根据Name获取Property
* @param name
* @return
*/
public static String getProperty(String name) {
return prop.getProperty(name);
} /**
* 获取所有的Property
* @return
*/
public static List<String> getBeanFactoryClass() {
List<String> list = new ArrayList<>();
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
list.add(prop.getProperty(key));
}
return list;
}
}

转载地址:https://www.cnblogs.com/hhmm99/p/9803119.html

内容很简单,但是感觉很有用

下面是Spring的通过@Value注解读取.properties配置内容

在Spring-dao.xml的配置文件中配置

<util:properties id="Prop" location="classpath:db.properties" />

内容

 salt=helloworld

使用注解获取配置内容

 //从db.properties文件中获取的值
@Value("#{Prop.salt}")
private String salt;

读取.Properties文件以及Spring注解读取文件内容的更多相关文章

  1. 【日常笔记】java spring 注解读取文件

    获取后缀文件 <!-- 注解读取properties文件开始 @Value("#{configProperties['userPageSize']}")private Str ...

  2. spring注解读取json文件

    开发时候在接口没有提供的时候,可以用json文件提前模拟接口数据 1.service层 package com.syp.spring.service; import java.io.File; imp ...

  3. Spring的属性文件properties使用注意

    Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...

  4. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  5. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  6. Spring 注解原理(三)@Qualifier @Value

    Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...

  7. spring 读取properties文件--通过注解方式

    问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...

  8. spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

    解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...

  9. spring boot --- 使用 注解 读取 properties 文件 信息

    1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...

随机推荐

  1. html 中使用 iconfont、fontAwesome

    在HTML中尽量使用iconfont 替代图片有很多好处,而且方便,可以设置大小.颜色 等 可以用于字体的设置. 一.使用iconfont 1.打开iconfont 官网 iconfont.cn. 2 ...

  2. DJango 基础(7)

    常用查询字段常用类型与模型关系表 知识点: 常用的查询 Field的常用参数 常用的模型字段类型 常用的查询 获取所有记录:rs = User.objects.all()获取第一条数据:rs = Us ...

  3. 【Django】RROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    刚刚启动项目的时候,突然报了这个错误.之前一直正常 后来百度一下,让我在window的host文件下,把被注释的127.0.0.1   localhost这个的注释取消 然鹅并木有用 直接用cmd连接 ...

  4. 100-days: eighteen

    Title: Why India's election is among the world's most expensive election n.选举,当选,选举权 expensive adj.昂 ...

  5. 2018-2019-2 20175234 实验一 Java开发环境的熟悉(Linux + IDEA)

    目录 20175234 实验一 Java开发环境的熟悉(Linux + IDEA) 第一部分 代码及运行结果截图 第二部分 要求 代码及截图 第三部分 题目 需求分析 设计 程序及运行结果 问题和解决 ...

  6. JavaSE基础知识(5)—面向对象(5.1类和对象概念、创建及内存分配)

    一.类和对象的相关概念 1.面向对象和面向过程的理解 面向对象和面向过程都属于解决问题的思考方式.面向过程:以执行者的角度思考问题,侧重于“怎么做”,比较适合解决小型项目面向对象:以指挥者的角度思考问 ...

  7. zabbix安装(Ubuntu)

    zabbix的安装 Zabbix监控架构至少需要server,agent,web模块.mysql.web部分和server安装在同一台机器上. Zabbix安装前服务器要做时间同步(ntp) 1.创建 ...

  8. Eigen使用矩阵作为函数参数

    1 使用矩阵作为函数参数介绍 文章来源Writing Functions Taking %Eigen Types as Parameters Eigen为了在函数中传递不同的类型使用了表达式模板技术. ...

  9. C++中的仿函数

    仿函数:实质就是重载了小括号(),通过类,定义一个对象,对象可以被实例化,具有内存可以存储数据,把需要比较的数据事先给到类对象的成员,这样在比较两个值的时候,可以只传入需要被比较的值即可.因为比较的值 ...

  10. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...