springboot 配置文件读取的两种方式,以及使用到的注解解释

了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息。

pom文件

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
    <!-- 单元测试使用 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.6.RELEASE</version>
    </dependency>
  </dependencies>

Spring-Boot读取配置文件的方式:

一.读取核心配置文件信息application.properties的内容

     核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

核心配置文件application.properties内容如下:

test.msg=Hello World SpringBoot
test.name=test
test.password= 123test

方式一:使用@Value方式(常用)

package cn.ar.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 9:57
 */
@RestController
//@PropertiesSource("classpath:application.properties")
public class FirstController { @Value("${test.name}") private String name; @Value("${test.password}") private String password; @RequestMapping(value = "/test") public String say(){ return name+":application.propertoes里面的name值"+"/t"+password+"密码"; } }

注意:1.在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。

2.@PropertySource配置文件路径设置,在类上添加注解,如果在默认路径下可以不添加该注解 ,我这里就在默认路径下,所以上面没用这个注解。
如果有多及目录, 比如 classpath:config/my.properties指的是src/main/resources目录下config目录下的my.properties文件.

3.多配置文件引用,若取两个配置文件中有相同属性名的值,则取值为最后一个配置文件中的值:

比如:@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})

访问:http://localhost:8080/test时得到:"方式一:test:application.propertoes里面的name值/t123test密码"

方式二:使用Environment方式

package cn.ar.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;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 9:57
 */
@RestController
public class FirstController {
   /* @Value("${test.name}")
    private String  name;
    @Value("${test.password}")
    private String password;

    @RequestMapping(value = "/test")
    public String  say(){
        return name+":application.propertoes里面的name值"+"/t"+password+"密码";
    }*/
   @Autowired
   private Environment env;
    @RequestMapping(value = "/test")
    public String  say(){
        return env.getProperty("test.name") + "      " +env.getProperty("test.password");
    }


}

注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。

二.读取自定义配置文件信息,例如:detifalManager.properties

  1. 首先建立对象与配置文件映射关系
  2. 方法中使用自动注入方式,将对象注入,调用get方法获取属性值
  3. 注意:新版本的@ConfigurationProperties没有了location属性,使用@PropertySource来指定配置文件位置
  4. prefix=”user1”指的是配置文件中的前缀,如user1.name,在定义对象属性名时为private String name;
  5. 读取配置文件中的集合时,使用List来接收数据,但List必须先实例化,负责会报错

为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件detifalManager.properties

resources/detifalManager.properties内容如下:

user1.name=admin11111111
user1.password=admin123qwqwq
user1.age= 15
user1.sex=男

创建管理配置的实体类:

package cn.ar.controller;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 10:24
 */
@Component//加上注释@Component,可以直接在其他地方使用@Autowired来创建其实列对象
@ConfigurationProperties(prefix = "user1")//设置配置文件的前缀
@PropertySource("classpath:detifalManager.properties")//设置自定义文件的路径
public class Manager {
    private String name;
    private String password;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

注意:
    在@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以user1.开头)
    使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

创建测试Controller

package cn.ar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: Jayden
 * Date: 2018/6/28
 * Time: 10:33
 */
@RestController
public class SecondController {
    @Autowired
    Manager manager;
    @RequestMapping("/test1")
    public String say(){
        return  "name"+ manager.getName() + "password" + manager.getPassword();
    }
}

注意:由于在manager类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。

访问:http://localhost:8080/test1时得到:"name admin11111111 password  admin123qwqwq"

猜你喜欢

转载自www.cnblogs.com/laosunlaiye/p/9237937.html