您好,欢迎访问一九零五行业门户网

在SpringBoot下怎么读取自定义properties配置文件

一、在resource中新建.properties文件
在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下。如图remote.properties所示
二、编写配置文件
remote.uploadfilesurl=/resource/files/remote.uploadpicurl=/resource/pic/
三、新建一个配置类remoteproperties.java
@configuration@configurationproperties(prefix = remote, ignoreunknownfields = false)@propertysource(classpath:config/remote.properties)@data@componentpublic class remoteproperties {  private string uploadfilesurl;  private string uploadpicurl;}
其中
@configuration 表明这是一个配置类
@configurationproperties(prefix = remote, ignoreunknownfields = false) 该注解用于绑定属性。prefix用来选择属性的前缀,也就是在remote.properties文件中的“remote”,ignoreunknownfields是用来告诉springboot在有属性不能匹配到声明的域时抛出异常。
@propertysource(classpath:config/remote.properties) 配置文件路径
@data 这个是一个lombok注解,用于生成getter&setter方法
@component 标识为bean
四、如何使用?
在想要使用配置文件的方法所在类上表上注解enableconfigurationproperties(remoteproperties.class)
并自动注入
@autowiredremoteproperties remoteproperties;
在方法中使用 remoteproperties.getuploadfilesurl()就可以拿到配置内容了。
@enableconfigurationproperties(remoteproperties.class)@restcontrollerpublic class testservice{  @autowired  remoteproperties remoteproperties;  public void test(){    string str = remoteproperties.getuploadfilesurl();    system.out.println(str);  }}
这里str就是配置文件中的”/resource/files/”了。
ps:下面看下 spring-boot中读取config配置文件的两种方式
了解过spring-boot这个技术的,应该知道spring-boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息。
spring-boot读取配置文件的方式:
一.读取核心配置文件信息application.properties的内容
     核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下:
test.msg=hello world springboot
方式一:使用@value方式(常用)
package solin.controller; import org.springframework.beans.factory.annotation.value; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class webcontroller {   @value(${test.msg})   private string msg;   @requestmapping(/index1)    public string index1(){     return 方式一:+msg;   } }
注意:在@value的${}中包含的是核心配置文件中的键名。在controller类上加@restcontroller表示将此类中的所有视图都以json方式显示,类似于在视图方法上加@responsebody。
访问:http://localhost:8088/index1时得到:方式一:hello world springboot
方式二:使用environment方式
package solin.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.core.env.environment; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class webcontroller {   @autowired   private environment env;      @requestmapping(/index2)    public string index2(){     return 方式二:+env.getproperty(test.msg);   } }
注意:这种方式是依赖注入evnironment来完成,在创建的成员变量private environment env上加上@autowired注解即可完成依赖注入,然后使用env.getproperty(键名)即可读取出对应的值。
访问:http://localhost:8088/index2时得到:方式二:hello world springboot
二.读取自定义配置文件信息,例如:author.properties
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件author.properties
resources/author.properties内容如下:
author.name=solin author.age=22
创建管理配置的实体类:
package solin.controller;  import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.configuration; import org.springframework.stereotype.component;  //加上注释@component,可以直接在其他地方使用@autowired来创建其实例对象 @component @configurationproperties(prefix = author,locations = classpath:author.properties)   public class mywebconfig{   private string name;   private int age;   public string getname() {     return name;   }   public void setname(string name) {     this.name = name;   }   public int getage() {     return age;   }   public void setage(int age) {     this.age = age;   } }
注意:
    在@configurationproperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)
    使用@component是让该类能够在其他地方被依赖使用,即使用@autowired注释来创建实例。
创建测试controller
package solin.controller; 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; @controller  public class configcontroller {   @autowired   private mywebconfig conf;      @requestmapping(/test)    public @responsebody string test() {     return name:+conf.getname()+---+age:+conf.getage();    } }
注意:由于在conf类上加了注释@component,所以可以直接在这里使用@autowired来创建其实例对象。
访问:http://localhost:8088/test时得到:name:solin---age:22
以上就是在springboot下怎么读取自定义properties配置文件的详细内容。
其它类似信息

推荐信息