- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个 AppProperties
带有 @ConfigurationProperties("app")
注释的 POJO .它包含不同的应用程序属性,我通常使用这些属性在 @Configuration
中配置我的 bean。类。@EnableConfigurationProperties(AppProperties.class)
使其可用于 @Autowiring
,这对于Java配置非常方便。
但是我的应用程序上下文的一部分是使用老式的 XML 配置配置的。我想知道如何访问@ConfigurationProperties AppProperties
XML配置中的bean?如果只有 @EnableConfigurationProperties
让我有能力提供 name
,我可以像这样在 XML 中使用 SpEL:#{appProperties.requiredProp}
(我真的很想实现它)。
不幸的是,我没有看到提供名称的方法,并且我尝试使用建议的 appProperties
名称失败:
SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
app-my.package.AppProperties
这并不明显,也从未明确说明过。
@EnableConfigurationProperties
我注意到以下内容:
@ConfigurationProperties
beans can be registered in the standard way (for example using@Bean
methods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties
) annotation.
AppProperties
的命名实例?从
application.properties
注入(inject)的值使用
@Bean
Java 中的方法或
<bean/>
在 XML 中?
AppProperties
到用XML配置的类,但我不认为这是一个好的设计解决方案,因为我只对单个
String
感兴趣来自
AppProperties
的属性(property).可能我不应该尝试在 XML 中使用这种“类型安全”的属性方式,而只是坚持旧方式:使用
${}
-分辨率,但我觉得我在
@ConfigurationProperties
中遗漏了一些东西概念所以请告诉我。
最佳答案
有两种注册配置属性 bean 的方法。
注册 @EnableConfigurationProperties
.
@EnableConfigurationProperties(AppProperties.class)
public class Application {}
@ConfigurationProperties("app")
public class AppProperties {}
@EnableConfigurationProperties
注解会自动注册
AppProperties
作为 bean 的类,称为
prefix-your.package.AppProperties
,当您按如下方式包含类时:
@EnableConfigurationProperties(AppProperties.class)
和
AppProperties
有一个前缀,
@ConfigurationProperties("app")
.
@Component
或
@Bean
@EnableConfigurationProperties
public class Application {}
@Component
@ConfigurationProperties("app")
public class AppProperties {}
@EnableConfigurationProperties
public class Application {
@Bean
@ConfigurationProperties("app")
public AppProperties appProperties() {
return new AppProperties();
}
}
@Component
和
@Bean
将注册一个名为
appProperties
的 bean .
#{appProperties.requiredProp}
关于spring - 如何通过 XML 配置中的名称访问 @ConfigurationProperties bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53999391/
我可以通过@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
我是一名优秀的程序员,十分优秀!