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 ...
随机推荐
- neutron openvswitch + vxlan 通讯
- JQuery Mobile - 解决切换页面时,闪屏,白屏等问题
在点击链接,切换页面时候,总是闪屏,感觉很别扭,看起来不舒服,怎么解决这个问题?方法很简单,就是在每个页面的meta标签内定义user-scalable的属性为 no! <meta name=& ...
- 《Python绝技:运用Python成为顶级黑客》 用Python实现免杀
1.免杀的过程: 使用Metasploit生成C语言风格的一些shellcode作为载荷,这里使用Windows bindshell,功能为选定一个TCP端口与cmd.exe进程绑定在一起,方便攻击者 ...
- Django signal 信号机制的使用
Django中提供了"信号调度",用于在框架执行操作时解耦,当某些动作发生的时候,系统会根据信号定义的函数执行相应的操作 一.Django中内置的 signal 类型主要包含以下几 ...
- C++中将二维数组(静态的和动态的)作为函数的参数传递
在C++编程中,我们经常将数组作为参数传递到另一个函数,数组的维数不同,传递方式也不同,此处将作一个总结,包括一维静态.动态数组,二维静态.动态数组. 一,一维数组(静态.动态一维数组) 1, 一维数 ...
- 萝卜保卫战3内购破解+Toast窗口增加(Love版)
涉及到一些不同的破解的方法,以及不同的破解思路,还有一些重要权限的删除等. 作者:HAI_ 这次目标是经常玩的萝卜保卫战,不知不觉,已经更新到3了.详细分析请参考https://bbs.ichunqi ...
- echarts4 主题切换
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- JPA总结——实体关系映射(一对多@OneToMany)
JPA总结——实体关系映射(一对多@OneToMany) 注意:本文出自“阿飞”的博客,如果要转载本文章,请与作者联系! 并注明来源: http://blog.sina.com.cn/s/blog_4 ...
- 六:MyBatis学习总结(六)——调用存储过程
一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...
- EL表达式中的11个隐式对象
EL表达式中定义了11个隐式对象,使用这些隐式对象可以很方便地读取到Cookie.HTTP请求消息头字段.请求参数.Web应用程序中的初始化参数的信息,EL表达式中的隐式对象具体如下: 隐式对象 作用 ...