【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定
我们通常会把一些经常变动的东西放到配置文件里。
比如之前写在配置文件application.properties
里的端口号server.port=8080
,另外常见的还有数据库的连接信息等等。
那么,我的数据库连接信息放在配置文件里,我要使用的话肯定得去解析配置文件,解析出的内容在 bean 里面去使用。
整个场景其实就是把配置文件里的所有配置,绑定到 java bean 里面。
要完成这个场景,基于 java 原生代码编写还是有点麻烦的。通常会做一个封装,读取到properties文件中的内容,并且把它封装到JavaBean中:
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean
... ...
}
}
这里就是使用Properties
类来加载配置文件a.properties
,然后遍历配置文件中的每一个k-v
,获取之后就可以用到对应的地方。
在 springboot 中简化了这个过程,这就是配置绑定。
配置绑定
通过使用注解@ConfigurationProperties
来完成配置绑定,注意需要结合@Component
使用。
新建一个组件Car
,有2个属性分别是品牌和价格:
@Component
public class Car {
private String brand;
private Integer price;
// get set tostring 就不贴了
在配置文件application.properties
,设置一些属性值,比如:
mycar.brand=QQ
mycar.price=9999
使用@ConfigurationProperties
注解,加到组件上:
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
... ...
传进去的 prefix 是配置文件里的前缀,这里就是 mycar。
验证
现在来测试一下是否绑定成功,在之前的HelloController
继续增加一个控制器方法:
@RestController
public class HelloController {
@Autowired
Car car;
@RequestMapping("/car")
public Car car() {
return car;
}
@RequestMapping("/hello")
public String Hello() {
return "Hello SpringBoot2 你好";
}
}
部署一下应用,浏览器访问http://localhost:8080/car
:
绑定成功。
另一种方式
除上述方法之外,还可以使用@EnableConfigurationProperties
+ @ConfigurationProperties
的方式来完成绑定。
注意,@EnableConfigurationProperties
注解要使用在配置类上,表示开启属性配置的功能:
//@ConditionalOnBean(name = "pet1")
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true)
@ImportResource("classpath:beans.xml") //配置文件的类路径
@EnableConfigurationProperties(Car.class) //开启属性配置的功能
public class MyConfig {
@Bean("user1")
public User user01(){
User pingguo = new User("pingguo",20);
pingguo.setPet(tomcatPet());
return pingguo;
}
@Bean("pet22")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
@EnableConfigurationProperties(Car.class)
传入要开启配置的类,这可以自动的把 Car 注册到容器中,也就是说之前 Car 上的@Component
就不需要了。
//@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
重新部署访问下地址,依然可以。
关于第二种的使用场景,比如这里的 Car 是一个第三方包里的类,但是人家源码没有加@Component
注解,这时候就可以用这种方式进行绑定。
最后,要记住当使用@ConfigurationProperties(prefix = "mycar")
这个配置绑定时,是跟 springboot 核心配置文件 application.properties
文件的内容建立的绑定关系。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY