@ConfigurationProperties配置参数注入

   1. pom中引入配置文件增加配置提示

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

2.properties/yml加入配置参数
zdf:
name: test123
age: 23
hobby:
game: LOL
study: java
3.创建Zdf类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "zdf")
public class Zdf {

private String name;
private Integer age;
private Hobby hobby;
}
4.创建Hobby类
package com.zdf.order.server;

import lombok.Data;

@Data
public class Hobby {

private String game;
private String study;
}
5.创建测试类
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class ConfController {

@Autowired
private Zdf zdf;

@GetMapping("/get")
public String get(){
return JSON.toJSONString(zdf);
}
}
6.结果

    

 

 

 

 

 

posted @ 2019-11-12 22:26  Melek  阅读(2423)  评论(0编辑  收藏  举报