[英]@Value Injection in Spring Boot 2.0.0 injects null
所以。 在阅读了官方文档后,我发现自己所做的事情没有错,我就没有想法了。
我的application.properties:
vz.info.version=0.2.8
我的属性组件
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;
}
我到处都是apiVersion
。 龙目岛似乎并不是问题所在。 我从文档中错过了什么?
编辑
我想这样称呼它:
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();
}
}
在检查通过注入环境使其运行之后,该属性也不存在。
您的类VZNFCTagResponse没有注册为Spring Bean。 将其注释为@Component并使用DI注入他。
避免使其成为组件并仍然受益于DI的唯一方法是将其标记为@Configurable
并启用加载时编织。 您能否将其注入创建VZNFCTagResponse
的组件中并将版本作为构造函数参数传递?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.