6.5. Properties
6.5.1. 载入*.properties文件
@RequestMapping("/config")@ResponseBodypublic void config() {try {Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("/config.properties"));for(String key : properties.stringPropertyNames()) {String value = properties.getProperty(key);System.out.println(key + " => " + value);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
6.5.2. @Value 注解
application.properties
server.name=Linux server.host=192.168.0.1,172.16.0.1
@Value("${server.name}")private String name;@Value("${server.name:Windows}") 如果application.properties没有配置server.name那么默认值将是 Windowsprivate String name;@Value("${app.name:@null}") // app.name = nullprivate String name;@Value("#{'${server.host}'.split(',')}") private List<String> host;
6.5.3. @PropertySource 注解
@PropertySource("classpath:/config.properties}") 忽略FileNotFoundException,当配置文件不存在系统抛出FileNotFoundException并终止程序运行,ignoreResourceNotFound=true 会跳过使程序能够正常运行 @PropertySource(value="classpath:config.properties", ignoreResourceNotFound=true)
载入多个配置文件
@PropertySources({ @PropertySource("classpath:config.properties"), @PropertySource("classpath:db.properties") })
test.properties
name=Neo age=30
package cn.netkiller.web;import java.util.Date;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller @PropertySource("classpath:test.properties") public class TestController {@AutowiredEnvironment environment;@Value("${age}")private String age;public TestController() {// TODO Auto-generated constructor stub}// 环境变量方式@RequestMapping("/test/env")@ResponseBodypublic String env() {String message = environment.getProperty("name");return message;}@RequestMapping("/test/age")@ResponseBodypublic String age() {String message = age;return message;}}
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。
6.5. Properties相关推荐
- More than one file was found with OS independent path 'META-INF/rxjava.properties
在app下的build.gradle里面的defaultConfig里面添加: defaultConfig { packagingOptions { exclu ...
- maven项目密码md5加密_加密Spring Boot中的application.properties
1 概述 什么?都2020年了还在Spring Boot的配置文件中写明文密码? (虽然是小项目,明文也没人看.) (明文简单快捷方便啊!!! ) (你看直接用户名root密码123456多么简单!! ...
- ItemAdding实现数据验证--中文字段,properties.AfterProperties值为null的问题
最近写事件接收器,发现中文字段如果直接用properties.AfterProperties["申请人"]这样获取的值为null,无法得到值.后拉忽然发现用英文字段可以得到值.难道 ...
- java error could_Java.lang.Error: Properties init: Could not determine current working directory.
用shell脚本编译项目,重新发布后,启动tomcat出现错误: Error occurred during initialization of VM java.lang.Error: Propert ...
- Java学习总结:57(Properties子类)
Properties子类 Properties类本身是Hashtable的子类,但是由于Properties类都使用String数据类型进行操作,所以在使用Properties类时主要使用本类所定义的 ...
- java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式
java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...
- C3P0_and_pro.properties配置文档代码
C3P0-config.xml配置文件 <c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config>< ...
- Properties持久的属性集
Properties 属性集合继承了Hashtable 属性包括属性名和属性值(键值对key==value) 作用 可以存储多个键值,与map相似 可以把键值对存储到文件中 可以把文件中的键值对读取到 ...
- 使用Properties连接数据库
使用Properties连接数据库 要注意的是: 1.通过配置文件来连接数据库时,连接信息要以 mysql.XXX开头,否则会提示异常. java.sql.SQLException: Access d ...
- Properties类读写配置文件
Properties类读写简单配置文件相当的方便,以前竟然没注意到这个类. FileInputStream fs = new FileInputStream("config.ini" ...
最新文章
- PCB雕刻钻孔机制作
- linux makefile 只能使用Tab键进行文本缩进
- 深入理解 SpringBoot 启动机制(starter 机制)
- 帝国php.ini路径,帝国CMS数据库配置文件是哪个文件?
- 求生之路 服务器优化参数,《求生之路2》服务器及网络参数优化指南
- IDA使用方法-----1
- linux的基础知识——UDP
- 对弈类游戏的人工智能(3)--博弈树优化
- docker基础——关于安装、常用指令以及镜像制作初体验
- 关于加域后win7、win8的C:\不能够新建文件,报0X0070522错误的解决方式
- iOS 读书笔记 第一章
- c语言中的内存分配malloc、alloca、calloc、malloc、free、realloc、sbr
- 【第三方软件】利用WIN8系统自带的绘图软件获取图像信息(位置和颜色信息)
- ReportMachine
- Linux聊天服务器
- linux敏感内容检测工具,认识Linux平台四大IDS***检测工具
- 做项目中遇到过得难点
- 计算机的串口波特率,串口常用参数
- 如何使用python刷博客浏览量---第一种方法
- CentOS上安装 Docker-CE以及Docker 加速器配置