Spring系列之——springboot解析resources.application.properties文件
摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfigurationProperties @Autowired @ConditionalOnProperty
1 准备
1.1 搭建springboot
1.2 写一个controller类
package com.gbm.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* Created by Administrator on 2019/2/20.
*/ @Controller
public class IndexController {
@RequestMapping("/index")
@ResponseBody
public String index() {
return "我爱北京天安门!";
}
}
2 几种获取属性值方式
配置application.properties
value.local.province=Zhejiang
value.local.city=Hangzhou complex.other.province=Jiangsu
complex.other.city=Suzhou
complex.other.flag=false
2.1 使用注解@Value("${xxx}")获取指定属性值
2.1.1 在controller类中获取属性值
package com.gbm.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* Created by Administrator on 2019/2/20.
*/ @Controller
public class IndexController {
@Value("${value.local.province}")
private String province; @Value("${value.local.city}")
private String city; @RequestMapping("/index")
@ResponseBody
public String index() {
StringBuffer sb = new StringBuffer();
sb.append(this.city);
sb.append(",");
sb.append(this.province);
sb.append(" ");
return sb.toString();
}
}
2.1.2 任何浏览器上运行 http://localhost:8080/index,结果如下

2.2 使用注解@ConfigurationProperties(prefix = "xxx")获得前缀相同的一组属性,并转换成bean对象
2.2.1 写一个实体类
package com.gbm.models; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* Created by Administrator on 2019/2/21.
*/
@Component
@ConfigurationProperties(prefix = "complex.other")
public class Complex {
private String province;
private String city;
private Boolean flag; public String getProvince() {
return province;
} public void setProvince(String province) {
this.province = province;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public Boolean getFlag() {
return flag;
} public void setFlag(Boolean flag) {
this.flag = flag;
} @Override
public String toString() {
return "Complex{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", flag=" + flag +
'}';
}
}
2.2.2 在controller类中获取属性值
package com.gbm.controller; import com.gbm.models.Complex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* Created by Administrator on 2019/2/20.
*/ @Controller
public class IndexController {
@Autowired
private Complex complex; @RequestMapping("/index")
@ResponseBody
public String index() {
return complex.toString();
}
}
2.2.3 在SpringBootApplication中使Configuration生效
package com.gbm.myspingboot; import com.gbm.models.Complex;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; @SpringBootApplication
@ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")})
@EnableConfigurationProperties(Complex.class)
@ConditionalOnProperty(value = "complex.other.town", matchIfMissing = true)
public class MyspingbootApplication { public static void main(String[] args) {
SpringApplication.run(MyspingbootApplication.class, args);
}
}
2.2.4 任何浏览器上运行 http://localhost:8080/index,结果如下

3 总结
关于解析application.properties文件,最重要的是对注解的使用,本文主要涉及到如下几个注解的运用
@Value("${xxx}")——获取指定属性值
@ConfigurationProperties(prefix = "xxx")——获得前缀相同的一组属性,并转换成bean对象
@EnableConfigurationProperties(xxx.class)——使Configuration生效,并从IOC容器中获取bean
@Autowired——自动注入set和get方法
@ConditionalOnProperty(value = "complex.other.town", matchIfMissing = true)——缺少该property时是否可以加载,如果是true,没有该property也会正常加载;如果是false则会抛出异常。例如
package com.gbm.myspingboot; import com.gbm.models.Complex;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; @SpringBootApplication
@ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")})
@EnableConfigurationProperties(Complex.class)
@ConditionalOnProperty(value = "complex.other.town", matchIfMissing = false)
public class MyspingbootApplication { public static void main(String[] args) {
SpringApplication.run(MyspingbootApplication.class, args);
}
}
matchIfMissing=false代码
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-21 23:30:56.532 ERROR 3152 --- [ main] o.s.boot.SpringApplication : Application run failed

Spring系列之——springboot解析resources.application.properties文件的更多相关文章
- springboot官网->application.properties文件
springboot application.properties 2.1.6.RELEASE
- SpringBoot中的application.properties外部注入覆盖
由想要忽略properties中的某些属性,引发的对SpringBoot中的application.properties外部注入覆盖,以及properties文件使用的思考. SpringBoot 配 ...
- springboot使用外部application.properties配置文件
一.背景介绍 springboot默认的application.properties文件只能在项目内部,如果打成docker镜像后配置文件也打进去了,这样每次需要改动配置(比如数据库的连接信息)就需要 ...
- SpringBoot读取application.properties文件
http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- 自定义Yaml解析器替换Properties文件
自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...
- springboot:读取application.yml文件
现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...
- application.properties文件中暗藏玄机
上次分享了如何一步一步搭建一个springboot的项目,详细参见<5分钟快速搭建一个springboot的项目>,最终的结果是在"8080"端口搭建起了服务,并成功访 ...
- springboot使用@Value注入properties文件中的值,中文乱码
最近开发一个需求,讲一个中文值配置在properties文件中,然后代码中使用@Value注解进行注入使用,然而出现了如下状况: 中文出现乱码,将代码修改如下: String str = new St ...
随机推荐
- [USACO5.1] 乐曲主题Musical Themes
题目链接:戳我 Emmm......hash怎么做啊不会啊 这里是SA后缀数组版本的 就是先两两做差分,作为要处理后缀的数组.普通地求出来h数组之后,我们二分这个答案,然后判定是否合法就行了.是否合法 ...
- django url 路由设置技巧
Django的url使用方法 利用Django开发站点.能够设计出很优美的url规则,假设url的匹配规则(包括正則表達式)组织得比較好,view的结构就会比較清晰.比較easy维护. 最简单的形式 ...
- 双向链表的实现——c++
c++实现双向链表 : #ifndef DOUBLE_LINK_HXX #define DOUBLE_LINK_HXX #include <iostream> using namespac ...
- webpack快速入门——集中拷贝静态资源
工作中会有一些已经存在但在项目中没有引用的图片资源或者其他静态资源(比如设计图.开发文档), 这些静态资源有可能是文档,也有可能是一些额外的图片.项目组长会要求你打包时保留这些静态资源, 直接打包到制 ...
- 实现可搜索仿select下拉选中
由于在优化项目中,发现先前写的一个活化石级的的可搜索下拉功能在高速搜索中会出现卡顿现象 1.起初的解决方法是在搜索事件中加入防抖函数隔一段时间才去触发他,同时搜索的不再是html文档片段,而是直接对数 ...
- iOS 11 application 新特性
1.- (void)applicationWillResignActive:(UIApplication *)application 说明:当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息 ...
- Jvm内存工具
1,JConsole 位于 [JDK] bin 下, 2,代码查看当前进程堆内存 long maxMemory = Runtime.getRuntime().maxMemory();long tot ...
- 转 ZFC公理系统
http://blog.sina.com.cn/s/blog_5d045b5c0100spld.html 首先,ZFC集合论中的公理大致分为3组: 1.外延公理. 2.子集公理模式.无序对公理.并集公 ...
- ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing 解决方案
我尝试的过程如下 1. 时间没有同步 用date命令看一下每个机器 如果时间差距大 说明确实有问题 ** 配置时间服务器 ** 检查时区 $ d ...
- 冒泡排序——Bubble Sort
基本思想:两个数比较大小,较大的数下沉,较小的数冒起来. 过程: 1.比较相邻的两个数据,如果第二个数小,就交换位置. 2.从后向前两两比较,一直到比较最前两个数据.最终最小数被交换到起始的位置,这样 ...