@Value 不提供来自应用程序属性 Spring Boot 的值 [英] @Value not providing values from application properties Spring Boot

查看:43
本文介绍了@Value 不提供来自应用程序属性 Spring Boot 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Springdoc 1.4 与 Spring-Boot 2.3 一起使用,并且在 OperationCustomizer 类中,我需要从应用程序属性文件中读取值.但是每次字段总是被初始化为null.规格如下

I'm using Springdoc 1.4 with Spring-Boot 2.3 and in the OperationCustomizer class, I need to read value from the application properties file. But everytime the field is always initialized to null. The specifications are as follows

application.properties

application.properties

application.security.authorization=true

OperationCustomizer 类

OperationCustomizer class

@Component
public class GlobalHeaderAdder implements OperationCustomizer {
    @Value("${application.security.authorization:true}")
    Boolean authFilterEnabled;       // <---- Initialized to NULL

    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
        System.out.println("____________________________\n" + authFilterEnabled + "\n+++++++++++++++++++++++++");
        if (authFilterEnabled) {
            operation.addParametersItem(new Parameter().$ref("#/components/parameters/ClientID"));
        }
        operation.addSecurityItem(new SecurityRequirement().addList("Authorization"));
        List<Parameter> parameterList = operation.getParameters();
        if (parameterList != null && !parameterList.isEmpty()) {
            Collections.rotate(parameterList, 1);
        }
        return operation;
    }
}

该类正在被下面的代码调用

The class is being called by the below code

@Bean
public GroupedOpenApi hideApis() {
    return GroupedOpenApi.builder().group("default")
            .pathsToExclude("/api/v2/**", "/v2/**")
            .pathsToMatch("/api/v1/**", "/v1/**")
            .addOperationCustomizer(new GlobalHeaderAdder())
            .build();
}

此处 提供的方法有效,但我希望有一种无需制作该字段的方法静态.

The approach provided here works but I would like to have an approach where I don't have to make the field static.

推荐答案

你的代码的问题是你自己通过调用构造函数来启动 GlobalHeaderAdder ,但在这种情况下它不是一个 spring bean,因此,@Value("${application.security.authorization:true}") 和所有其他 spring 注释将不起作用.

issue with your code is that you initiate GlobalHeaderAdder by your own via invoking constructor, but in that case it's not a spring bean, and, as a result, @Value("${application.security.authorization:true}") and all other spring annotations will not work.

所以,为了解决这个问题,你应该注入 GlobalHeaderAdder bean 来创建 hideApis:

so, to fix the issue, you should either inject GlobalHeaderAdder bean for creating hideApis:

@Bean
public GroupedOpenApi hideApis(GlobalHeaderAdder globalHeaderAdder) {
    return GroupedOpenApi.builder().group("default")
        .pathsToExclude("/api/v2/**", "/v2/**")
        .pathsToMatch("/api/v1/**", "/v1/**")
        .addOperationCustomizer(globalHeaderAdder)
        .build();
}

或者按照下面的方式创建一个bean,你就可以按照上面提到的方式注入hideApis(对于这个选项,你不需要@ComponentGlobalHeaderAdder 类下:

or create a bean in the following way, that you will be able to inject into hideApis in the way mentioned above (for this option you don't need to have @Component under class GlobalHeaderAdder):

@Bean
public GlobalHeaderAdder globalHeaderAdder() {
    return new GlobalHeaderAdder();
}

这篇关于@Value 不提供来自应用程序属性 Spring Boot 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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