【问题标题】:Spring Boot doesn't pick the values from application.ymlSpring Boot 不会从 application.yml 中选择值
【发布时间】:2020-08-05 13:05:16
【问题描述】:

我正在使用 spring-boot-starter-web 最新版本 2.2.6.RELEASE。但是我的应用程序总是为带有 @value 注释的变量返回 null。

我的 build.gradle,

plugins {
  id 'com.google.cloud.tools.jib' version '2.1.0'
  id 'org.springframework.boot' version '2.2.6.RELEASE'
  id 'io.spring.dependency-management' version '1.0.8.RELEASE'
  id 'java'
  id 'eclipse'
  id 'idea'
  id 'maven-publish'
}

jar {
    archiveBaseName = 'my-project'
    project.version =  '1.0.0'
}

repositories {
    jcenter()
    mavenCentral()
    mavenLocal()
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '+'
    implementation('org.springframework.boot:spring-boot-starter-web:+') {
        exclude module: 'spring-boot-starter-tomcat'
    }

    implementation("org.springframework.boot:spring-boot-starter-jetty:+")
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0'
    implementation group: 'io.kubernetes', name: 'client-java', version: '+'
    implementation group: 'com.google.cloud', name: 'google-cloud-dataproc', version: '+'

    implementation(project(':my-project-1')) {
        exclude group: 'org.yaml'
    }

    implementation project(':my-project2')
}

tasks.withType(JavaCompile) {
    options.compilerArgs << '-Xlint:unchecked'
    options.deprecation = true
}

我的 Java 代码,

@Component
public class MyClass {

    @Value("${dataproc.host}")
    private static String dataprocEndpoint;

我的应用程序.yml

dataproc:
  host: dataproc.googleapis.com:443

我的应用程序.java,

@SpringBootApplication
@EnableSwagger2
@Configuration
public class Application {

    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
    private transient String PATTERNS_TO_HIDE = "^/(?!error|autoconfig|beans|profile|info|health|pause|env|refresh|resume|restart|actuator|dump|mappings|trace|metrics|heapdump|archaius|configprops|features|liquibase|loggers|auditevents).*$";

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Docket api() {
        ApiInfo apiInfo = new ApiInfoBuilder().license("Proprietary").title("My Application")
                .description("My Application").build();

        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex(PATTERNS_TO_HIDE)).build().pathMapping("/").apiInfo(apiInfo)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, newArrayList(new ResponseMessageBuilder().code(500)
                        .message("500 message").responseModel(new ModelRef("Error")).build()));
    }
}

启动服务器时总是为空, dataprocEndpoint: null

我在这里缺少什么配置?

请在此处提供您的意见。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:
    1. 用引号括住您的财产:

      数据处理: 主机:'dataproc.googleapis.com:443'

    Yml 处理器将您的配置属性名称解释为 dataproc.host.dataproc.googleapis.com.443

    1. 从您的字段声明中删除静态。

      @Value("${dataproc.host}") 私有字符串 dataprocEndpoint;

    静态字段通过类加载进行初始化。 Spring bean 后处理器无法在这么早的阶段注入值。如果您想将值注入静态字段,则可以使用 setter 注入。

    参考https://mkyong.com/spring/spring-inject-a-value-into-static-variables/

    【讨论】:

    • 即使使用单引号更新后仍然为空
    • 更新了我的答案
    • 创建 bean 类而不是实例类后它正在工作。
    • 酷!很高兴为您提供帮助。
    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 2017-04-04
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多