Spring 获取配置文件的值

package com.hafiz.www.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.*;
import java.util.Properties; /**
* Desc:properties文件获取工具类
* Created by hafiz.zhang on 2016/9/15.
*/
public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
private static Properties props;
static{
loadProps();
} synchronized static private void loadProps(){
logger.info("开始加载properties文件内容.......");
props = new Properties();
InputStream in = null;
try {
       <!--第一种,通过类加载器进行获取properties文件流-->
in = PropertyUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
       <!--第二种,通过类进行获取properties文件流-->
//in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
props.load(in);
} catch (FileNotFoundException e) {
logger.error("jdbc.properties文件未找到");
} catch (IOException e) {
logger.error("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("jdbc.properties文件流关闭出现异常");
}
}
logger.info("加载properties文件内容完成...........");
logger.info("properties文件内容:" + props);
} public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
} public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}

Spring boot 获取配置文件的值

使用注解  @Value

@Controller
public class TestController { @Value("${server.port:Hello World22}")
private String message = "Hello World"; @RequestMapping("/test")
public void test(){
System.out.println("message:"+message);
}
}

spring 获取配置文件的值的更多相关文章

  1. mybatis整合spring获取配置文件信息出错

    描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...

  2. asp.net core 如何在Controller获取配置文件的值

    场景:我们会把一些配置信息,写在配置文件文件中,便于我们修改和配置.在之前的asp.net 中可以通过ConfigurationManger来获取web.config里面的配置.在.net core ...

  3. spring 读取配置文件,将值注入到静态字段

    resources/config/config-dev.properties es.ip.node=xxxxxxxcluster.name=xxxxxxxclient.transport.sniff= ...

  4. C# 获取配置文件节点值

    <?xml version="1.0" encoding="utf-8" ?><configuration>  <appSetti ...

  5. Spring Boot入门(二):获取配置文件值

    本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...

  6. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  7. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContex ...

  8. 获取Spring项目配置文件元素

    在开发中有时候要获取配置文件里的值,通常可以利用如下方式来读取: public class PropertyUtil { private static Properties p = new Prope ...

  9. Spring 获取propertise文件中的值

    Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...

随机推荐

  1. Java实验——输出一个数组里面连续子数组最大的和(二)文件操作

    在本周的练习中,主要是对上周的实验进行健壮性的完善,即在所能考虑到的情况之中,尽量使自己的程序能够正常地运行. 在上周的实验中,我已经是在编程过程中考虑到用户输入的错误类型的问题,所以这一方面并没有多 ...

  2. SQLAlchemy的group_by和order_by的区别

    1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...

  3. POJ2752 Seek the Name, Seek the Fame 【KMP】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Ac ...

  4. 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题

    当我在运行一个基于scrapy的爬虫时出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordina ...

  5. request.getAttribute()与request.setAttribute()

    request.getAttribute()与request.setAttribute() request.getAttribute("nameOfObj")可得到JSP页面一表单 ...

  6. I2S简单学习

    以下只是个人看法,有不妥之处,请批评指出. 参考资料:http://blog.csdn.net/ce123_zhouwei/article/details/6919954: 一.I2S接口简述 I²S ...

  7. 本人会linux系统的各种版本的安装,近期发教程

    小弟虽然刚刚踏入职场,可是咱大学也不是打酱油过的啊,研究过各种版本系统的安装,也都均已经实践,勿喷,有问题 咱们可以相互探讨!

  8. Android-shareSDK

    1.当数据: 地址:http://sharesdk.mob.com/Download 2.集成数据: DOS命令: java -jar QuickIntegrater.jar    (输入自己的项目名 ...

  9. The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

    打开表单偶尔会出现这个提示,解决方法: web.config增加配置: <configuration> <system.net> <settings> <ht ...

  10. PHP 7.2 RC3 on CentOS/RHEL 7.3 via Yum

    https://webtatic.com/packages/php72/ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-lat ...