摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfigurationProperties @Autowired @ConditionalOnProperty

1 准备

1.1 搭建springboot

1.2 写一个controller类

  1. package com.gbm.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. /**
  8. * Created by Administrator on 2019/2/20.
  9. */
  10.  
  11. @Controller
  12. public class IndexController {
  13. @RequestMapping("/index")
  14. @ResponseBody
  15. public String index() {
  16. return "我爱北京天安门!";
  17. }
  18. }

2 几种获取属性值方式

  配置application.properties

  1. value.local.province=Zhejiang
  2. value.local.city=Hangzhou
  3.  
  4. complex.other.province=Jiangsu
  5. complex.other.city=Suzhou
  6. complex.other.flag=false

2.1 使用注解@Value("${xxx}")获取指定属性值

  2.1.1 在controller类中获取属性值

  1. package com.gbm.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7.  
  8. /**
  9. * Created by Administrator on 2019/2/20.
  10. */
  11.  
  12. @Controller
  13. public class IndexController {
  14. @Value("${value.local.province}")
  15. private String province;
  16.  
  17. @Value("${value.local.city}")
  18. private String city;
  19.  
  20. @RequestMapping("/index")
  21. @ResponseBody
  22. public String index() {
  23. StringBuffer sb = new StringBuffer();
  24. sb.append(this.city);
  25. sb.append(",");
  26. sb.append(this.province);
  27. sb.append(" ");
  28. return sb.toString();
  29. }
  30. }

  2.1.2 任何浏览器上运行 http://localhost:8080/index,结果如下  

  

2.2 使用注解@ConfigurationProperties(prefix = "xxx")获得前缀相同的一组属性,并转换成bean对象

  2.2.1 写一个实体类

  1. package com.gbm.models;
  2.  
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5.  
  6. /**
  7. * Created by Administrator on 2019/2/21.
  8. */
  9. @Component
  10. @ConfigurationProperties(prefix = "complex.other")
  11. public class Complex {
  12. private String province;
  13. private String city;
  14. private Boolean flag;
  15.  
  16. public String getProvince() {
  17. return province;
  18. }
  19.  
  20. public void setProvince(String province) {
  21. this.province = province;
  22. }
  23.  
  24. public String getCity() {
  25. return city;
  26. }
  27.  
  28. public void setCity(String city) {
  29. this.city = city;
  30. }
  31.  
  32. public Boolean getFlag() {
  33. return flag;
  34. }
  35.  
  36. public void setFlag(Boolean flag) {
  37. this.flag = flag;
  38. }
  39.  
  40. @Override
  41. public String toString() {
  42. return "Complex{" +
  43. "province='" + province + '\'' +
  44. ", city='" + city + '\'' +
  45. ", flag=" + flag +
  46. '}';
  47. }
  48. }

  2.2.2 在controller类中获取属性值

  1. package com.gbm.controller;
  2.  
  3. import com.gbm.models.Complex;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8.  
  9. /**
  10. * Created by Administrator on 2019/2/20.
  11. */
  12.  
  13. @Controller
  14. public class IndexController {
  15. @Autowired
  16. private Complex complex;
  17.  
  18. @RequestMapping("/index")
  19. @ResponseBody
  20. public String index() {
  21. return complex.toString();
  22. }
  23. }

  2.2.3 在SpringBootApplication中使Configuration生效

  1. package com.gbm.myspingboot;
  2.  
  3. import com.gbm.models.Complex;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.ComponentScans;
  10.  
  11. @SpringBootApplication
  12. @ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")})
  13. @EnableConfigurationProperties(Complex.class)
  14. @ConditionalOnProperty(value = "complex.other.town", matchIfMissing = true)
  15. public class MyspingbootApplication {
  16.  
  17. public static void main(String[] args) {
  18. SpringApplication.run(MyspingbootApplication.class, args);
  19. }
  20. }

  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则会抛出异常。例如

  1. package com.gbm.myspingboot;
  2.  
  3. import com.gbm.models.Complex;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.ComponentScans;
  10.  
  11. @SpringBootApplication
  12. @ComponentScans({@ComponentScan("com.gbm.controller"), @ComponentScan("com.gbm.models")})
  13. @EnableConfigurationProperties(Complex.class)
  14. @ConditionalOnProperty(value = "complex.other.town", matchIfMissing = false)
  15. public class MyspingbootApplication {
  16.  
  17. public static void main(String[] args) {
  18. SpringApplication.run(MyspingbootApplication.class, args);
  19. }
  20. }

matchIfMissing=false代码

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2019-02-21 23:30:56.532 ERROR 3152 --- [ main] o.s.boot.SpringApplication : Application run failed

Spring系列之——springboot解析resources.application.properties文件的更多相关文章

  1. springboot官网->application.properties文件

    springboot application.properties 2.1.6.RELEASE

  2. SpringBoot中的application.properties外部注入覆盖

    由想要忽略properties中的某些属性,引发的对SpringBoot中的application.properties外部注入覆盖,以及properties文件使用的思考. SpringBoot 配 ...

  3. springboot使用外部application.properties配置文件

    一.背景介绍 springboot默认的application.properties文件只能在项目内部,如果打成docker镜像后配置文件也打进去了,这样每次需要改动配置(比如数据库的连接信息)就需要 ...

  4. SpringBoot读取application.properties文件

    http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...

  5. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  6. 自定义Yaml解析器替换Properties文件

    自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...

  7. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  8. application.properties文件中暗藏玄机

    上次分享了如何一步一步搭建一个springboot的项目,详细参见<5分钟快速搭建一个springboot的项目>,最终的结果是在"8080"端口搭建起了服务,并成功访 ...

  9. springboot使用@Value注入properties文件中的值,中文乱码

    最近开发一个需求,讲一个中文值配置在properties文件中,然后代码中使用@Value注解进行注入使用,然而出现了如下状况: 中文出现乱码,将代码修改如下: String str = new St ...

随机推荐

  1. Python 获取秒级时间戳与毫秒级时间戳

    原文:Python获取秒级时间戳与毫秒级时间戳 1.获取秒级时间戳与毫秒级时间戳 import time import datetime t = time.time() print (t) #原始时间 ...

  2. 用 python 修改文件中指定的行数

    #! /bin/python filename='setup.ini' lines=[] with open(filename,'r') as f: lines=f.readlines() lines ...

  3. 《Python绝技:运用Python成为顶级黑客》 用Python进行渗透测试

    1.编写一个端口扫描器 TCP全连接扫描.抓取应用的Banner #!/usr/bin/python #coding=utf-8 import optparse import socket from ...

  4. position:absolute元素 怎样居中

    <div style = 'height:20px;position:absolute;z-index:9999;top:0;left:0;right:0;margin:auto;'> & ...

  5. ajax post 400 bad request

    是前端ajax没有加声明:contentType:'application/json',

  6. express 重新加载

    1,res.location() 2. res.redirect() location()与redirect()的比较: Express的response对象,是对Node.js原生对象ServerR ...

  7. 一对一关联查询注解@OneToOne的实例详解

    表的关联查询比较复杂,应用的场景很多,本文根据自己的经验解释@OneToOne注解中的属性在项目中的应用.本打算一篇博客把增删改查写在一起,但是在改的时候遇到了一些问题,感觉挺有意思,所以写下第二篇专 ...

  8. C#里面获取web和非web项目路径

    非Web程序获取路径几种方法如下: 1.AppDomain.CurrentDomain.BaseDirectory  2.Environment.CurrentDirectory 3.HttpRunt ...

  9. css单行文本及多行文本溢出显示省略号

    关于文本溢出的相关属性: 1. text-overflow: clip|ellipsis|string;   该属性规定当文本溢出包含元素时发生的事情. clip : 修剪文本. ellipsis : ...

  10. AngularJS入门之Services

    关于AngularJS中的DI 在开始说AngularJS的Service之前,我们先来简单讲讲DI(Dependency Injection,通常中文称之为“依赖注入”). DI是一种软件设计模式, ...