博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot——读取.properties配置文件解决方案
阅读量:2036 次
发布时间:2019-04-28

本文共 4041 字,大约阅读时间需要 13 分钟。

解决方案

Spring Boot 读取properties配置文件时,默认读取的是application.properties。 

方法一:@ConfigurationProperties注解方式

@Component 表示将该类标识为Bean

@ConfigurationProperties(prefix = "demo")用于绑定属性,其中prefix表示所绑定的属性的前缀。

@PropertySource(value = "config.properties")表示配置文件路径。

import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties;//import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * @date 2017年6月1日 下午4:34:18  * @version V1.0 * @since JDK : 1.7 */@Component@ConfigurationProperties(prefix = "com.zyd")// PropertySource默认取application.properties// @PropertySource(value = "config.properties")public class PropertiesConfig {    public String type3;    public Map
login = new HashMap
(); public List
urls = new ArrayList<>(); public String getType3() { return type3; } public String getTitle3() { try { return new String(title3.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return title3; } public Map
getLogin() { return login; } public void setLogin(Map
login) { this.login = login; } public List
getUrls() { return urls; } public void setUrls(List
urls) { this.urls = urls; }}

方法二:@Value注解方式

import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @date 2017年6月1日 下午3:49:30  * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction {    @Value("${com.zyd.type}")    private String type;    /**     *      * 第二种方式:使用`@Value("${propertyName}")`注解     *      */    @RequestMapping("/value")    public Map
value() throws UnsupportedEncodingException { Map
map = new HashMap
(); map.put("type", type); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}

注:如果@Value${}所包含的键名在application.properties配置文件中不存在的话,会抛出异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.name}"

 方法三:Environment

import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction {    @Autowired    private Environment env;    /**     *      * 第三种方式:使用`Environment`     *      */    @RequestMapping("/env")    public Map
env() throws UnsupportedEncodingException { Map
map = new HashMap
(); map.put("type", env.getProperty("com.zyd.type2")); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}

方法四:PropertiesLoaderUtils

参考文章

 

转载地址:http://srgaf.baihongyu.com/

你可能感兴趣的文章
【JS】【31】读取json文件
查看>>
Ubuntu Boost 编译
查看>>
OpenCV模块分析(转)
查看>>
聊聊我对写好程序的认识
查看>>
OpenSSL源代码学习[转]
查看>>
插件原理2[转自CSDN]
查看>>
OpenCV Windows7 VC6.0安装以及HelloWorld
查看>>
python升级导致yum命令无法使用的解决办法
查看>>
vi/vim 中如何在每行行首或行尾插入指定字符串
查看>>
linux 查看端口被哪个程序占用
查看>>
socket
查看>>
Spring下载地址
查看>>
Linux日志2
查看>>
VS的路径变量[转]
查看>>
MFC消息处理[转]
查看>>
cookie被禁止后怎样使用session的解决方案
查看>>
Eclipse 部分快捷键失效解决
查看>>
Bootstrap 自定义弹框
查看>>
MyBatis 分页插件 PageHelper 使用方法
查看>>
AbstractQueuedSynchronizer 源码分析
查看>>