欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring系列之——springboot解析resources.application.properties文件

程序员文章站 2022-12-16 14:56:47
本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfigurationProperties @Autowired @ConditionalOnProperty ......

  摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@value @configurationproperties @enableconfigurationproperties @autowired @conditionalonproperty

 

1 准备

1.1 

1.2 写一个controller类

Spring系列之——springboot解析resources.application.properties文件
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 "我爱北京*!";
    }
}
view code

2 几种获取属性值方式

  配置application.properties

Spring系列之——springboot解析resources.application.properties文件
value.local.province=zhejiang
value.local.city=hangzhou

complex.other.province=jiangsu
complex.other.city=suzhou
complex.other.flag=false
view code

2.1 使用注解@value("${xxx}")获取指定属性值

  2.1.1 在controller类中获取属性值

Spring系列之——springboot解析resources.application.properties文件
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();
    }
}
view code

  2.1.2 任何浏览器上运行 ,结果如下  

  Spring系列之——springboot解析resources.application.properties文件

2.2 使用注解@configurationproperties(prefix = "xxx")获得前缀相同的一组属性,并转换成bean对象

  2.2.1 写一个实体类

Spring系列之——springboot解析resources.application.properties文件
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类中获取属性值

Spring系列之——springboot解析resources.application.properties文件
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();
    }
}
view code

  2.2.3 在springbootapplication中使configuration生效

Spring系列之——springboot解析resources.application.properties文件
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 任何浏览器上运行 ,结果如下 

  Spring系列之——springboot解析resources.application.properties文件

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则会抛出异常。例如

Spring系列之——springboot解析resources.application.properties文件
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文件