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. Oracle一次Insert多条数据

    insert all into JK_TB_DATE (fbmmc,fgzjh,fsbmc,fsbxh,fsbbh,db_shuifenyi,db_pihao,db_wuliaobianma) ',' ...

  2. python--第十天总结(线程、进程和协程)

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

  3. 【POJ2778】DNA Sequence 【AC自动机,dp,矩阵快速幂】

    题意 题目给出m(m<=10)个仅仅由A,T,C,G组成的单词(单词长度不超过10),然后给出一个整数n(n<=2000000000),问你用这四个字母组成一个长度为n的长文本,有多少种组 ...

  4. centos7 smplayer 安装 安装视频播放器

    # yum -y install http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noar ...

  5. c#npoi 报错Cannot get a numeric value from a text cell 的解决

    一般是因为cell里边的值为数字导致,有时变成文本格式还是解决不了这个问题. 下边的代码是c# 改变设置cell类型的方法 是用这个参数 CellType.String Row.GetCell((in ...

  6. shell脚本编写informix数据库中表的导入和导出

    表的导入: 第一行:是指此脚本使用/bin/bash来解释执行. 第四行:定义一个list,里面存放表的名称,之间用空格隔开. 第七行:dbaccess tofpe(数据库名) <<EOF ...

  7. 20172306《Java程序设计与数据结构》第一周总结

    20172306<Java程序设计>第一周学习总结 教材学习内容总结 本周主要学习<Android和Java>书中的第二十三章和第二十六章. 第二十三章:Android简介 A ...

  8. ping内网一台虚拟机延时很大(hyper-v虚拟机)的解决办法

    问题现象: ping 内网一台虚拟机延时很大,不稳定,造成业务系统响应慢.查看服务器上各种资源都正常. 解决办法: 在物理机上找到和hyper-v绑定的那个网卡,把“虚拟机队列”禁用掉就好了,如下图: ...

  9. GUI学习之〇——PyQt5安装

    GUI(Graphical User Interface)是程序和软件使用者的接口,好的GUI是一个良好的软件的前提,在这里演示一下用PyQt5做一个GUI的方法 软件需求:python3.6 用的是 ...

  10. Delphi 域名解析为IP地址

    花生壳:1.LJSZForm-Lable1-Caption改成 “IP地址或域名:”2.LJSZForm-BitBtn1Click-注释掉--else if IsIP(Trim(IPEdit.Text ...