在没有 Spring 的情况下注入应用程序属性 [英] Inject application properties without Spring

查看:33
本文介绍了在没有 Spring 的情况下注入应用程序属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like a simple, preferably annotation-based way to inject external properties into a java program, without using the spring framework (org.springframework.beans.factory.annotation.Value;)

SomeClass.java

SomeClass.java

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

应用程序.yml

some:
  property:
    name: someValue

在标准库中是否有推荐的方法来执行此操作?

Is there a recommended way to do this in the standard library?

推荐答案

我最终使用了 apache公共配置:

pom.xml:

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

src/.../PropertiesLoader.java

src/.../PropertiesLoader.java

PropertiesConfiguration config = new PropertiesConfiguration();
config.load(PROPERTIES_FILENAME);
config.getInt("someKey");

/src/main/resources/application.properties

/src/main/resources/application.properties

someKey: 2

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

I did not want to turn my library into a Spring application (I wanted @Value annotations, but no application context + @Component, extra beans, extra Spring ecosystem/baggage which doesn't make sense in my project).

这篇关于在没有 Spring 的情况下注入应用程序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆