Spring系列之——springboot解析resources.appli…
2019-02-25 16:10:00来源:博客园 阅读 ()
摘要:本文通过讲解如何解析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); } }
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
原文链接:https://www.cnblogs.com/guobm/p/10415795.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash