Spring Boot 学习 (五)@ConfigurationProperties 和 @Value 自定义参数绑定
大家在开发过程中,有时可能会遇到这样的场景,我们想要读取我们的配置文件信息,读取并封装到一个实体类中,这样我们在代码中使用配置参数就相对方便了很多。SpringBoot 为我们提供的注解 @ConfigurationProperties 和 @Value,便可以很方便的帮我们实现配置参数的绑定功能。那么接下来我们将介绍一下这两者怎么使用,以及有什么区别:
一、@ConfigurationProperties 注解方式
1)自定义配置类
如果我们所要绑定的类是我们自定义的,我们可以使用以下方法绑定:
a. 首先我们的配置文件application.properties中的配置信息如下:
[code]myproperties.userName = LINCO myproperties.password = 123456
b.接下来我们编写一个自定义的配置类MyProperties.java , 在自定义配置类中加上如下注解:
[code]@ConfigurationProperties(prefix = "myproperties") #这里的prefix是我们在application.properties中定义的前缀 #SpringBoot的@ConfigurationProperties已经为我们设置了默认值prefix #所以这里也可以直接省略为@ConfigurationProperties("myproperties")
[code]package com.colinlin.hellospringboot.hellodemo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 自定义配置类 * * @author Colin Lin * @create 2018-09-09 10:47 **/ @Component @ConfigurationProperties(prefix = "myproperties") public class MyProperties { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
c.我们写一个Controller测试一下我们的绑定结果:
[code]package com.colinlin.hellospringboot.hellodemo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @Autowired MyProperties myProperties; @RequestMapping("/hello") @ResponseBody public String hello(){ return "UserName: " + myProperties.getUserName() + " Password: " + myProperties.getPassword(); } }
d.启动我们的程序,并且访问我们的的controller:localhost:8080/hello,可以得到以下结果与我们配置文件中的信息一致:
UserName: LINCO Password: 123456
2)第三方提供的组件类
上面的绑定方法只针对我们自定的类才可以这样做,当我们想要绑定的类是第三方提供的,我们无法直接在类中加注解,此时我们可以用下面的方法实现参数绑定(当然以下方法也适用于自定义类的参数绑定):
把@ConfigurationProperties直接定义在@bean的注解上,这时我们先假设自定义的实体类MyProperties是第三方提供的(注意:此时在MyProperties类中不需要@Component和@ConfigurationProperties了)
[code]package com.colinlin.hellospringboot; import com.colinlin.hellospringboot.hellodemo.MyProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; @SpringBootApplication public class HellospringbootApplication { /*第三方组件类绑定参数,也适用于自定义类*/ @Bean @ConfigurationProperties("myproperties") public MyProperties getMyProperties(){ return new MyProperties(); } /*第三方组件类绑定参数,也适用于自定义类*/ public static void main(String[] args) { SpringApplication.run(HellospringbootApplication.class,args); } }
此时我们重新启动程序,访问localhost:8080/hello可以看到与第一种方法一样都可以拿到我们的的配置信息,这里不再重复阐述。
二、@Value 注解方式
@Value 注解方式需要一个一个属性对应使用@Value("${propertiesName}")指定。我们仍旧使用上面的MyProperties类进行演示,代码修改如下:
[code]package com.colinlin.hellospringboot.hellodemo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 自定义配置类 * * @author Colin Lin * @create 2018-09-09 10:47 **/ @Component public class MyProperties { @Value("${myproperties.userName}") private String userName; @Value("${myproperties.password}") private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
此时运行我们的程序,访问localhost:8080/hello依然可以拿到我们的配置参数值。
三、@ConfigurationProperties 和 @Value 的区别
@ConfigurationProperties | @Value | |
---|---|---|
注解功能 | 可以批量注入配置文件中的属性 | 只能一个个指定注入属性 |
松散语法绑定(Relaxed binding) | 支持 | 不支持 |
EL表达式 | 不支持 | 支持 |
JSR303数据校验 @Validated | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
附:
阅读更多所谓松散语法也就是属性命名规则(Relaxed binding)
- person.firstName:使用标准方式 - person.first-name:大写用- - person.first_name:大写用_ - PERSON_FIRST_NAME: 系统属性推荐使用这种写法
- SpringBoot中自定义参数绑定
- SpringBoot学习之配置文件注入@ConfigurationProperties
- Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
- 学习Spring boot 注解 @Value @Component @ConfigurationProperties
- SpringBoot中自定义参数绑定
- SpringBoot下自定义参数绑定步骤解析
- SpringBoot中自定义properties文件配置参数并带有输入提示
- SpringBoot 学习记录(八): properties 属性自定义
- SpringBoot中自定义参数绑定步骤详解
- spring boot 学习使用@ConfigurationProperties注解
- Spring Boot第四弹:Spring Boot之@Value和@ConfigurationProperties
- Spring-boot实例学习之 自定义starter
- Spring Boot学习之路——自定义拦截器
- 在Spring Boot中使用 @ConfigurationProperties 注解
- 在spring-boot中使用@ConfigurationProperties
- 解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."
- Spring Boot 之 @ConfigurationProperties
- 25. Spring Boot使用自定义的properties【从零开始学Spring Boot】
- 【系统学习SpringBoot】再遇Spring Data JPA之JPA应用详解(自定义查询及复杂查询)
- Spring Boot 之 @ConfigurationProperties