【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定
2021/9/2 23:36:23
本文主要是介绍【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
文件的内容建立的绑定关系。
这篇关于【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)