读取.Properties文件以及Spring注解读取文件内容
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注解读取文件内容的更多相关文章
- 【日常笔记】java spring 注解读取文件
获取后缀文件 <!-- 注解读取properties文件开始 @Value("#{configProperties['userPageSize']}")private Str ...
- spring注解读取json文件
开发时候在接口没有提供的时候,可以用json文件提前模拟接口数据 1.service层 package com.syp.spring.service; import java.io.File; imp ...
- Spring的属性文件properties使用注意
Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...
- 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity
文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- Spring 注解原理(三)@Qualifier @Value
Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...
- spring 读取properties文件--通过注解方式
问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...
- spring boot --- 使用 注解 读取 properties 文件 信息
1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...
随机推荐
- 使用jQuery+huandlebars中with应用及with+this应用
兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) w ...
- (转)VS2010反编译dll之后存在的resource修改为resx
https://www.cnblogs.com/tangbaono1/p/6897183.html 1.找到安装VS的时候,存在的ResGen.exe,我的电脑是win7的,路径是在C:\Progra ...
- [Ting's笔记Day4]将Ruby on Rails项目部署到Heroku
今天想笔记的是把自己写的Ruby on Rails项目部署(Deploy)到Heroku! Heroku是Salesforce公司旗下的云端服务商,支持多种程序语言像是Ruby,PHP,Python等 ...
- 数据导入Excel时,出现ole error 800AC472这个错误,怎么解决。
我也出现过这个问题 在生成报表的时候不要动EXCEL中的任何单元格 让它完成保存就可以了 或者是把office 2003 删除下载一个office 2000就可以解决 据说是版本兼容的问题 不是高手 ...
- 微信小程序页面跳转 的几种方式
最近在做微信小程序,碰到页面跳转的问题,总结一下页面之间跳转的方式 一.wx.navigateTo(OBJECT) 这是最普遍的一种跳转方式,其官方解释为:“保留当前页面,跳转到应用内的某个页面” 类 ...
- cdnbest里如何查看网站是否被缓存
比如开启了强制缓存,如何查看缓存是否生效 本例以firefox浏览器查看,先打开浏览器,按下F12, 然后在浏览器是输入网址访问 如下图响应头里的 x-cache显示 Miss from 就是没有缓 ...
- python 迭代多个对象
并行迭代 zip for a,b,c in zip(list,list,tuple,list): print a,b,c 串行迭代 itertools.chain a = [1,2,3,4,5] b ...
- Linux - 文件和目录
文件和目录(理解) 目标 理解 Linux 文件目录的结构 01. 单用户操作系统和多用户操作系统(科普) 单用户操作系统:指一台计算机在同一时间 只能由一个用户 使用,一个用户独自享用系统的全部硬件 ...
- Javascript的算法题目
用js实现单链表的增删,直接上代码 const linkList=new LinkList() function LinkList(){ var Node=function(element){ thi ...
- AI大道理头尾标识
标题 点击上方“AI大道理”,选择“置顶”公众号 重磅干货,深入讲解AI大道理 —————— 正文 —————— 浅谈则止,深入理解AI大道理 扫描下方“AI大道理”,选择“关注”公众号 欢迎加入!