Spring系列之——springboot解析resources.application.properties文件
摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@value @configurationproperties @enableconfigurationproperties @autowired @conditionalonproperty
1 准备
1.1
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 任何浏览器上运行 ,结果如下
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 任何浏览器上运行 ,结果如下
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); } }
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
上一篇: 百度SEO精准流量的获取玩法
下一篇: 自媒体玩转五步运营开通今日头条原创短视频
推荐阅读
-
Spring系列之——springboot解析resources.application.properties文件
-
spring-boot-2.0.3不一样系列之番外篇 - springboot事件机制,绝对有值得你看的地方
-
源码系列【springboot之@Import注解多个类引入同一个类源码解析】
-
【Spring系列】SpringBoot+Mybatis实现excel文件的下载方式
-
SpringBoot系列(四)SpringBoot 之 Spring Data Jpa 支持
-
SpringBoot2.x系列教程(四十六)Spring Boot集成WebSocket之STOMP协议简介
-
SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾
-
Spring系列之——springboot解析resources.application.properties文件
-
spring-boot-2.0.3不一样系列之番外篇 - springboot事件机制,绝对有值得你看的地方
-
源码系列【springboot之@Import注解多个类引入同一个类源码解析】