@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.结果

    

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM