java - 在没有主应用程序的情况下从 application.properties 获取值(value)

标签 java spring-boot

我在一个 Spring Boot 项目中工作,该项目包括几个使用嵌入式 Tomcat 启动的应用程序和一个仅用作运行应用程序的库的独特应用程序。在该库应用程序中,我需要从 application.properties 文件中检索变量的值。作为一个库应用程序,它没有 Main 类,因此该变量将始终为 null。 我的尝试是:

 public class AuthAdapter implements IAuthAdapter {

    @Value("${signin.key}")
    private String key;

    @Override
    public String getSigningKey() {
        return key;
    }
}

当使用它的应用程序调用 getSigninKey() 时,变量键为空。 考虑到前面解释的情况,我该如何用 appliaction.properties 文件中的值填充变量键?

最佳答案

您需要根据您的要求使用@Service/@Component/@Repository/@Configuration 注释您的AuthAdapter 类。通过这样做,spring boot 将在 spring 上下文初始化期间将 application.properties 注入(inject)到您的类中。

关于java - 在没有主应用程序的情况下从 application.properties 获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55342071/

相关文章:

java - 仅当用户不滚动 RecyclerView 时才绑定(bind) ViewHolders

java - 如何使用 JodaTime 将本地时间转换为上午/下午时间格式?

java - 如何让 Cassandra、Spring Boot 调用实体的字段 setter ?

java - Spring Boot 过滤 : At least one property must be given

java - 为什么@Table注解在spring的CRUD操作中是可选的

java - 我如何在 Java 中执行以下 curl 命令

java - 将 Color 对象的 "round"转换为最近的 Color Constant 的最佳方法是什么?

java - 如何在 createStartScripts 任务中更改 unixStartScriptGenerator.template 以便 distTar 在 build.gradle 中使用我的自定义模板文件?

java - JPAQuery 与 Spring Pageable

java - 将使用 ManyToMany 关系的 SQL 查询转换为 JPQL 查询