[英]In spring-boot Class constructor get called first or bootstrap.yml?
I Have a spring-boot application. 我有一个spring-boot应用程序。 I wanted to pick some values from bootstrap.yml
into the constructor of the class. 我想从bootstrap.yml
选择一些值到类的构造函数中。 Below is the code Snippet. 下面是代码片段。
public class MapicsSCFFGeneratorServiceImpl implements MapicsSCFFGeneratorService {
@Value("${azuresb.nameSpace}")
private String nameSpace;
@Value("${azuresb.sasPolicyKeyName}")
private String sasPolicyKeyName;
@Value("${azuresb.sasPolicyKey}")
private String sasPolicyKey;
@Value("${azuresb.serviceBusRootURI}")
private String serviceBusRootURI;
@Value("${azuresb.queueName}")
private String queueName;
public MapicsSCFFGeneratorServiceImpl() {
config = ServiceBusConfiguration.configureWithSASAuthentication(nameSpace, sasPolicyKeyName, sasPolicyKey,
serviceBusRootURI);
}
}
My question is Which one get call first bootstrap.yml or constructor 我的问题是哪一个首先调用bootstrap.yml或构造函数
Because If I am printing this values inside constructor I am getting Null on the other hand outsides the constructor the values are printing. 因为如果我在构造函数中打印这个值,另一方面我在得到Null的构造函数外部正在打印值。
It makes sense that the constructor be invoked before the Spring processing that values the fields with Spring properties. 有意义的是,在Spring处理之前调用构造函数,该函数使用Spring属性对字段进行值。
From a logical point of view, the constructor has to be invoked before Spring values instance fields. 从逻辑的角度来看,必须在Spring值实例字段之前调用构造函数。
As alternative, you could move the processing that uses the fields valued by Spring in a method annotated with javax.annotation.@PostConstruct
. 或者,您可以在使用javax.annotation.@PostConstruct
注释的方法中移动使用Spring值的字段的处理javax.annotation.@PostConstruct
。
From the specification : 从规格:
The
PostConstruct
annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.PostConstruct
注释用于在完成依赖项注入以执行任何初始化之后需要执行的方法。
@PostConstruct
public void postProcess(){
config = ServiceBusConfiguration.configureWithSASAuthentication(nameSpace, sasPolicyKeyName, sasPolicyKey,
serviceBusRootURI);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.