使用@ConfigurationProperties读取application.properties和application.yml

application.yml内容

db:
  url: 127.0.0.1
  port: 7788
  name: root
  pwd: 123456

application.properties内容

db.url=127.0.0.1:7788
db.name=root
db.pwd=12345678

读取配置 方式一

创建bean类

类名上由以下注解修饰:

  • @Component:将该类实例化,并加载到spring容器中,然后才能生效使用
  • @ConfigurationProperties(prefix = "db"),表示读取配置文件,需要配置prefix 前缀参数, 此处是:db
package top.malaoshi.bean;

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

@Component
@ConfigurationProperties(prefix = "db")
public class DbProperties {
    private String url;
    private String name;
    private String pwd;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

使用

比如在 controller 中读取并显示

package top.malaoshi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.bean.DbProperties;

@RestController
public class DbController {

    @Autowired
    private DbProperties db;

    @GetMapping("/db")
    public String db(){
        return String.format("url:%s--name:%s--pwd:%s", db.getUrl(),db.getName(),db.getPwd());
    }
}

读取配置 方式二

创建bean类

类名上由注解@ConfigurationProperties(prefix = "db")修饰,表示读取配置文件,需要配置prefix 前缀参数, 此处是:db

注意: 此方式不用@Component注解

package top.malaoshi.bean;

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

@ConfigurationProperties(prefix = "db")
public class DbProperties {
    private String url;
    private String name;
    private String pwd;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

使用@EnableConfigurationProperties注解

如果一个类只使用@ConfigurationProperties注解,为了让该类实例化,加载到spring容器中,需要在 启动类 或 配置类 加上注解@EnableConfigurationProperties(类名.class)

@SpringBootApplication
@EnableConfigurationProperties(DbProperties .class)
public class StdsecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(StdsecurityApplication.class, args);
    }

}

使用

比如在 controller 中读取并显示

package top.malaoshi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.bean.DbProperties;

@RestController
public class DbController {

    @Autowired
    private DbProperties db;

    @GetMapping("/db")
    public String db(){
        return String.format("url:%s--name:%s--pwd:%s", db.getUrl(),db.getName(),db.getPwd());
    }
}

原文出处:/show_1EF5rAR3rN87.html