gpt4 book ai didi

java - 在没有 Spring 的情况下注入(inject)应用程序属性

转载 作者:行者123 更新时间:2023-12-04 16:58:31 25 4
gpt4 key购买 nike

我想要一种简单的、最好是基于注释的方法来将外部属性注入(inject)到 java 程序中,而不使用 spring 框架 (org.springframework.beans.factory.annotation.Value;)

SomeClass.java

@Value("${some.property.name}")
private String somePropertyName;

应用程序.yml
some:
property:
name: someValue

在标准库中有推荐的方法吗?

最佳答案

我最终使用了 apache commons configuration :

pom.xml:

<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>

src/.../PropertiesLoader.java
PropertiesConfiguration config = new PropertiesConfiguration();
config.load(PROPERTIES_FILENAME);
config.getInt("someKey");

/src/main/resources/application.properties
someKey: 2

我不想把我的库变成一个 Spring 应用程序(我想要 @Value 注释,但没有应用程序上下文 + @Component 、额外的 bean、额外的 Spring 生态系统/行李,这在我的项目中没有意义)。

关于java - 在没有 Spring 的情况下注入(inject)应用程序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com