摘要:本文通过讲解如何解析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
如对本文有疑问, 点击进行留言回复!!
解决idea中出现“illegal character U+200B” 问题
荐 为什么加了@Transactional注解,事务没有回滚?
Attribute ‘sklearn.linear_model._logistic.LogisticRegression.multi_class‘ must be explicitly set to
Java/Python实现 LeetCode剑指Offer 14-I.剪绳子(动态规划)
网友评论