博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring系列之——springboot解析resources.application.properties文件
阅读量:4347 次
发布时间:2019-06-07

本文共 5685 字,大约阅读时间需要 18 分钟。

  摘要:本文通过讲解如何解析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. */@Controllerpublic class IndexController {    @RequestMapping("/index")    @ResponseBody    public String index() {        return "我爱北京天安门!";    }}
View Code

2 几种获取属性值方式

  配置application.properties

value.local.province=Zhejiangvalue.local.city=Hangzhoucomplex.other.province=Jiangsucomplex.other.city=Suzhoucomplex.other.flag=false
View Code

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. */@Controllerpublic 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();    }}
View Code

  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 +                '}';    }}
View Code

  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. */@Controllerpublic class IndexController {    @Autowired    private Complex complex;    @RequestMapping("/index")    @ResponseBody    public String index() {        return complex.toString();    }}
View Code

  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);    }}
View Code

  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);    }}
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

 

转载于:https://www.cnblogs.com/guobm/p/10415795.html

你可能感兴趣的文章
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>