@EnableConfigurationProperties 与 @ConfigurationProperties
一.@ConfigurationProperties的作用
该注解配合其属性prefix,可以在项目中的配置文件中读取对应的配置信息。
但是在springboot中,所有的实例必须要注册到容器中,才能够使用springboot的相关功能。而@ConfigurationProperties是不自带注册功能的,因此,还需要其他注解配合使用才能够真正的将配置文件中的内容绑定到对应的类中。可供选择的注解有两个,下面演示如何使用。
二.使用@Component注解+@ConfigurationProperties,将其注入到容器中。
1.yml文件
user:
name: admin
age: 18
2.创建User对象,用以映射配置文件中的相关配置
package com.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
//注意,@ConfigurationProperties注解的类如果没有注入到容器中是无法读取yml配置中的内容的
@Component
//该注解的意思就是,从配置文件中读取user前缀的配置
//请注意prefix属性的值,
//@ConfigurationProperties注解只会从配置文件中对应的prefix中获取值
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
private String age;
}
3.请求入口
package com.controller;
import com.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private User user;
@GetMapping
public void getUser() {
System.out.println(user);
}
}
4.运行结果如下:
如果在User对象中没有加@Component注解,则启动项目时控制台会出现以下提示:
二.使用@EnableConfigurationProperties注解+@ConfigurationProperties,将其注入到容器中。
@EnableConfigurationProperties有两种用法:简洁用法和标准用法。
1.简洁用法
1.1 去除@Component注解
1.2添加@EnableConfigurationProperties(User.class)
package com.controller;
import com.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
//直接在要使用User对象的类上标注@EnableConfigurationProperties注解
@EnableConfigurationProperties(User.class)
public class UserController {
@Resource
private User user;
@GetMapping
public void getUser() {
System.out.println(user);
}
}
1.3运行结果如下:
2.标准用法,使用配置类的方式进行注入
2.1修改yml文件,添加other配置
user:
name: admin
age: 18
other:
one: 1
two: 2
2.2添加Other类
package com.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "other")
public class Other {
private String one;
private String two;
}
2.3编写配置类,标准用法的优势在于,可以在一个类中配置多个读取配置的Bean
package com.config;
import com.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties
public class UserConfig {
@Bean
public User user() {
return new User();
}
@Bean
public Other other() {
return new Other();
}
}
2.4 请求入口
package com.controller;
import com.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
//直接在要使用User对象的类上标注@EnableConfigurationProperties注解
@EnableConfigurationProperties(User.class)
public class UserController {
@Resource
private User user;
@Resource
private Other other;
@GetMapping
public void getUser() {
System.out.println(user);
System.out.println(other);
}
}
运行结果如下:
此处直接在Controller上使用了@EnableConfigurationProperties是为了方便演示,在实际使用中应该在对应的业务层中使用该注解。