Spring Boot get configuration file value

In the process of program development, some values that may be modified are often put into the configuration file. How to get the values of the configuration file in Spring Boot?

This blog explains how to use @ Value annotation or @ ConfigurationProperties annotation to get the Value of configuration file

Method 1: use @ Value annotation to get the profile Value

First, add the following configuration in application.yml:

book:
  author: wangyunfei
  name: spring boot

Then modify the code of the Spring Boot project startup class as follows:

package com.zwwhnly.springbootdemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringbootdemoApplication {

    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.name}")
    private String bookName;

    @RequestMapping("/")
    String index() {
        return "book name is:" + bookName + " and book author is:" + bookAuthor;
    }

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

Start the project, enter http://localhost:8080 / in the browser, and you will see the following information:

Mode 2: use @ ConfigurationProperties annotation to get the configuration file value

In method 1, we use @ Value annotation to get the configuration file Value, but if we need to get it in multiple places, we need to write annotation in multiple places, causing confusion and poor management,

In fact, Spring Boot also provides @ ConfigurationProperties annotation to get the configuration file value. This method can automatically associate the configuration file value with a Bean, which is more convenient to use. I suggest this method.

Add the following configuration in application.yml:

author:
  name: wangyunfei
  age: 32

Create a new class AuthorSettings, and add @ Component and @ ConfigurationProperties annotations

package com.zwwhnly.springbootdemo;

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

@Component
@ConfigurationProperties(prefix = "author")
public class AuthorSettings {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

Modify the startup class code as follows:

package com.zwwhnly.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringbootdemoApplication {

    @Autowired
    private AuthorSettings authorSettings;

    @RequestMapping("/")
    String index() {
        return "book name is:" + authorSettings.getName() + " and book author is:" + authorSettings.getAge();
    }

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

Start the project, enter http://localhost:8080 / in the browser, and you will see the following information:

Tags: Java Spring

Posted on Mon, 02 Dec 2019 03:41:35 -0500 by predhtz