简体   繁体   English

Spring Boot 2.0.0中的@Value注入会注入null

[英]@Value Injection in Spring Boot 2.0.0 injects null

So. 所以。 After reading the official docs and finding nothing wrong with what I am doing, I just ran out of ideas. 在阅读了官方文档后,我发现自己所做的事情没有错,我就没有想法了。

My application.properties: 我的application.properties:

vz.info.version=0.2.8

My properties Component 我的属性组件

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class VZProperties {

  @Value("${vz.info.version}")
  @Getter
  @Setter
  private String apiVersion;

}

I am getting null all over the place for the apiVersion . 我到处都是apiVersion And Lombok does not seem to be the issue. 龙目岛似乎并不是问题所在。 What did I miss from the docs? 我从文档中错过了什么?

EDIT 编辑

I would like to call it like this: 我想这样称呼它:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import lombok.Getter;

import java.io.Serializable;
import java.util.List;

public class VZNFCTagResponse implements Serializable{

  private static final long serialVersionUID = -2824767225275894898L;

  @Autowired
  private VZProperties properties;

  public VZNFCTagResponse(List<VZNFCTagAction> tagList){
    this.tags = tagList;
  }

  /*...*/

  @JsonProperty
  public String apiVersion(){
    return this.properties.getApiVersion();
  }      
}  

And after having checked to get it running via injecting Environment, the property isn't there, either. 在检查通过注入环境使其运行之后,该属性也不存在。

Your class VZNFCTagResponse is not registered as Spring Beans. 您的类VZNFCTagResponse没有注册为Spring Bean。 Annotate it as @Component and use DI for inject him. 将其注释为@Component并使用DI注入他。

The only way to avoid making it into a component and still benefit from DI is to mark it @Configurable and enable load-time weaving. 避免使其成为组件并仍然受益于DI的唯一方法是将其标记为@Configurable并启用加载时编织。 Can't you just inject it into the component that creates VZNFCTagResponse and pass the version as a constructor parameter? 您能否将其注入创建VZNFCTagResponse的组件中并将版本作为构造函数参数传递?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM