- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们实际上使用 Spring Boot 的 @ConfigurationProperties
作为基本上的配置映射器:它为我们提供了在对象上映射属性的简单快捷方式。
@ConfigurationProperties("my.service")
public class MyService {
private String filePrefix;
private Boolean coefficient;
private Date beginDate;
// getters/setters mandatory at the time of writing
public void doBusinessStuff() {
// ...
}
}
尽管在我们制作应用程序原型(prototype)时,这极大地提高了工作效率,但我们开始质疑这是否是正确的用法。
我的意思是,配置属性在 Spring Boot 的上下文中具有不同的状态,它们通过执行器端点公开,它们可用于触发条件 bean,并且似乎更面向技术配置属性。
问题:在任何业务属性/值上使用此机制是否“正确”,还是纯粹的滥用?
我们错过了任何潜在的缺点吗?
现在我们唯一担心的是我们不能在不可变类上使用@ConfigurationProperties
,这与Spring Boot跟踪器上的这个问题密切相关:Allow field based @ConfigurationProperties binding
最佳答案
如果您的属性代表可根据环境/配置文件进行配置的内容,则该机制就是为此而设计的。虽然我有点不清楚你的意思“在对象上映射属性”。
一般来说,我不喜欢这种风格,特别是当您的 bean 有多个属性需要设置时。更标准的习惯用法是使用一个类来封装用于创建 bean 的属性/设置:
@ConfigurationProperties("my.service")
public class MyServiceProperties {
private String filePrefix;
private Boolean coefficient;
private Date beginDate;
// getters/setters mandatory at the time of writing
}
那么你的服务类将如下所示:
@EnableConfigurationProperties(MyServiceProperties.class)
public class MyService {
@Autowired
private MyServiceProperties properties;
//do stuff with properties
public void doBusinessStuff() {
// ...
}
}
这至少允许您通过其构造函数轻松地将属性传递到不可变类中(复制任何可变属性)。如果您发现应用程序的其他部分需要一些共享配置,还可以重用属性 bean。
关于java - Spring Boot @ConfigurationProperties 正确用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30073582/
我可以通过@ConfigurationProperties注释看到以下警告 使用 @ConfigurationProperties 时,建议将“spring-boot-configuration-pr
我正在尝试找到一种方法来在上下文启动之前记录我的应用程序属性(通过 @ConfigurationProperties 注入(inject)到我的 bean 中),以便我可以在初始化所有 bean 之前
我有一个 ApplicationProperties,我在我的服务中使用它。当我在测试中模拟其余模板来测试该服务时,当我将 applicationProperties.getBebusiness().
@Value 和 @ConfigurationProperties 注入(inject)有什么区别?为什么 Value 属性经过验证而 ConfigurationProperties 不经过验证? 最
我正在尝试使用 .NET 配置并了解自定义部分、元素等。 似乎实现这些自定义部分需要显式声明 getter 和 setter,通常会导致代码膨胀。 例如这里: http://msdn.microsof
有一种行为我找不到相关文档。让我们假设以下代码。它应该在控制台中显示使用 foo.bar 属性配置的内容: @SpringBootApplication @Component public class
我正在开发一个 Spring Integration/Boot 应用程序。我使用多文档 application.yml (src/main/resources/application.yml) 来设置
我有一个看起来像这样的父类(super class) public class MyProvider { private Integer id; private ProviderAcc
我想在程序中读/写(并保存)应用程序的配置文件 app.config 是这样的: ... 当我使用 ConfigurationManager.G
使用@ConfigurationProperties定义属性时,是否可以定义特定字段的前缀而不是整个类? 例如,假设我们有一个 Properties 类 @ConfigurationPropertie
如果 bean 类来自外部库(例如我自己的公共(public)库),我如何使用 @ConfigurationProperties 填充该 bean? 示例:来自公共(public)库: package
.yml 文件 cassandra: keyspaceApp:junit solr: keyspaceApp:xyz bean @Component @ConfigurationPro
Spring Boot 的 @ConfigurationProperties 是否可以拥有不可变(最终)字段?注解?下面的例子 @ConfigurationProperties(prefix = "e
我在 application.properties 文件的一个属性中有一个值列表: my-property=abc,def,ghi 然后我将它加载到我的 @ConfigurationPropertie
我有多个 .yml project: module1: mysql: urls project: module2: mysql:
如何加载配置以创建继承默认值并支持覆盖的 Shape 列表? 这是我的 application.yml 文件的样子... store: default: color: red si
以下是 spring boot 项目中 YML 文件中的条目。 application: applicationPath: "/demo" defaultFilePath: ${applica
当通过@ConfigrationProperties编写spring配置时,有没有一种方法可以连接配置的根值。 我的配置是这样的 foo.bar=hello foo.bar.baz=world 如何从
我使用 @ImportResource({"classpath:property-sources.xml"}) 注释来外部化某些配置文件的路径。 因此,在应用程序中,我可以使
我希望 Spring Boot 从 application.properties 中的条目读取我的配置,但它们未激活。我的 application.properties 包含以下条目: foo.use
我是一名优秀的程序员,十分优秀!