Spring Boot reads properties

Keywords: Programming SpringBoot Spring Attribute Java

Original address: http://www.yiidian.com/springboot/springboot-properties.html

There are two comments in Spring Boot that read the property values of the application.properties or application.yml file.

  1. @Value
  2. @ConfigurationProperties

1 @Value

1.1 Basic Types

1) Configuration

nickname=eric
age=20

2) SpringBoot Read Configuration

/**
 * @Value Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
public class ConfigController {

    //1.1 Read Basic Types
    @Value("${nickname}")
    private String nickname;
    @Value("${age}")
    private Integer age;

1.2 JavaBean Types

1) Configuration

user.nickname=jack
user.age=18

2) SpringBoot Read Configuration

/**
 * @Value Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
public class Config2Controller {

    //1.2 Read JavaBean Types
    @Value("${user.nickname}")
    private String nickname;
    @Value("${user.age}")
    private Integer age;
		```
### 1.3 Array/List Collection

1)To configure
```properties
user.list=eric,jack,rose

2) SpringBoot Read Configuration

/**
 * @Value Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
public class Config3Controller {

    //1.3 Read Array/List Collection Type
    @Value("#{'${user.list}'.split(',')}")
    private List<String> list;

1.4 Map Collection

1) Configuration

user.map={nickname:'eric',age:20}

2) SpringBoot Read Configuration

/**
 * @Value Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
public class Config4Controller {

    //1.4 Read Map Collection Type
    @Value("#{${user.map}}")
    private Map<String,Object> map;

2 @ConfigurationProperties

2.1 Basic Types

1) Configuration

nickname=eric
age=20

2) SpringBoot Read Configuration

/**
 * @ConfigurationProperties Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
@ConfigurationProperties(prefix = "")
public class Config5Controller {

    //1.1 Read Basic Types
    private String nickname;
    private Integer age;
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

Note the following points:

  • Prefix: The prefix that represents the attribute if the user.nickname prefix is user Property name must be and
  • Property files have consistent property names Property must provide a setter method to inject the property value of the file

2.2 JavaBean Types

1) Configuration

user.nickname=jack
user.age=18

2) SpringBoot Read Configuration

/**
 * @ConfigurationProperties Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
@ConfigurationProperties(prefix = "user")
public class Config6Controller {

    //1.2 Read JavaBean Types
    private String nickname;
    private Integer age;

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

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

2.3 Array/List Collection

1) Configuration

user.list2[0]=eric
user.list2[1]=jack
user.list2[2]=rose

2) SpringBoot Read Configuration

/**
 * @ConfigurationProperties Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
@ConfigurationProperties(prefix = "user")
public class Config7Controller {

    //1.3 Read Array/List Collection Type
    private List<String> list2;
    public void setList2(List<String> list2) {
        this.list2 = list2;
    }

2.4 Map Collection

1) Configuration

user.map2.nickname=eric
user.map2.age=20
```properties
2)SpringBoot Read Configuration
```java
/**
 * @ConfigurationProperties Annotation - Read application.properties properties
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
@ConfigurationProperties(prefix = "user")
public class Config8Controller {

    //1.4 Read Map Collection Type
    private Map<String,Object> map2;
    public void setMap2(Map<String, Object> map2) {
        this.map2 = map2;
    }

Source download: https://pan.baidu.com/s/1eAwxjQClVt3TxuqtL6VbIA

Welcome to my public number: A little tutorial.Get exclusively organized learning resources and daily dry delivery. If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Posted by dinger on Thu, 27 Feb 2020 09:18:41 -0800