摘要:本文通过讲解如何解析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文件的更多相关文章

  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. jzoj5804

    這道題n-m很小,可以從此入手 記f[i][j]為i個字符括號綜合為j的合法方案數 則第i個括號可以枚舉為(和),所以f[i][j]=f[i-1][j-1]+f[i-1][j+1],小心越界 再記a為 ...

  2. jmeter测试计划配置

    用户定义的变量: 测试计划上可以添加用户定义的变量.一般添加一些系统常用的配置.如果测试过程中想切换环境,切换配置,一般不建议在测试计划上添加变量,因为不方便启用和禁用,一般是直接添加用户自定义变量组 ...

  3. java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了)

    java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了)  目标效果(命令行启动服务): service xxxxd start #启动服务  servic ...

  4. 剑指offer三十一之连数中1出现的次数(从1到n整数中1出现的次数

    一.题目 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  5. (转)防止人为误操作MySQL数据库技巧一例

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/1321061 防止人 ...

  6. Java之集合(七)Map

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7327216.html 1.前言 按照顺序,本章本是要对Set的相关类进行介绍及讲解的.但是对于其实现有所了解的都 ...

  7. java内存数据管理

    准确的说应该是java8以前的内存管理方式 区别在永久代(方法区)上 public class RamManager { //1.a存储于永久代 public static int a =1; pri ...

  8. 【Java并发编程】:使用wait/notify/notifyAll实现线程间通信

    在java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  9. Android中ListView的使用步骤

    第一步:  首先,在 布局文件中,声明listView控件. <ListView android:id="@+id/lv" android:layout_width=&quo ...

  10. 《垃圾回收的算法与实现》——GC复制算法

    基本概念 GC复制算法将堆分成From和To两个内存块,当From被占满时GC将From中的存活对象复制到To中,同时将From和To交换. 通过递归遍历GC root(即采用深度优先)复制存活对象, ...