@Configuration,@ConfigurationProperties,@EnableConfigurationProperties
@Configuration
- API: https://www.javadoc.io/doc/org.springframework/spring-context/5.0.7.RELEASE
- @Configuration+@Value
- @Configuration+@bean
- @Configuration+@Import
- @Configuration+@Profile
- @Configuration+@ImportResource
@ConfigurationProperties
不能单独使用必须使用带有可以实例化bena的注解比如:@Component或者组合了@Component的注解
@ConfigurationProperties配合@EnableConfigurationProperties使用
可以实现@ConfigurationProperties所注解类是否实例化,由@EnableConfigurationProperties所注解的类决定。
@Data
@ConfigurationProperties(prefix="my.test")
public class MyServerConfiguration {
private String name;
}
@Component
@EnableConfigurationProperties(MyServerConfiguration.class)
public class MyServerConfigutation2 {
}
MyServerConfigutation2这个类如果没有实例化MyServerConfiguration这个配置类也不会实例化!
联合使用
@Configuration,@ConfigurationProperties配合@ConditionalOnProperty使用,实现效果:
@Configuration
@ConditionalOnProperty(prefix = "my.test", value = "enabled", havingValue = "true")
@EnableConfigurationProperties(MyServerConfiguration.class)
public class MyServerConfigutation2 {
}
@Data
@ConfigurationProperties(prefix="my.test")
public class MyServerConfiguration {
private String name;
}
只有配置文件中指定了:my.test.enabled=true才会实例化MyServerConfigutation2 这个类,也就才会实例化MyServerConfiguration ;
让自己变得更优秀才可以有更多资源
分类:
spring boot的应用场景
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?