简体   繁体   English

在spring-boot类构造函数中调用first或bootstrap.yml?

[英]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.

相关问题 bootstrap.yml 未在 Spring Boot 2 中加载 - bootstrap.yml not loading in Spring Boot 2 无法从spring-boot中的Properties文件和Bootstrap.yml中选择值 - Unable to pick the values from Properties file and Bootstrap.yml in spring-boot 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别? - What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? Spring 引导:使用 bootstrap.yml 加载通用配置应用程序属性文件 - Spring Boot: Load common config application properties file using bootstrap.yml 在 Spring 上未找到/读取 Bootstrap.yml 部署在 Tomcat 上的 Boot 2 应用程序 - Bootstrap.yml not being found/read on Spring Boot 2 application deployed on Tomcat 创建依赖于Spring Boot并具有自己的bootstrap.yml的库 - Create Library that has a dependency on Spring Boot that has a it's own bootstrap.yml 是否需要 bootstrap.yml? - Is bootstrap.yml required? Spring Cloud 2020.0 不再处理 bootstrap.yml 配置 - bootstrap.yml configuration not processed anymore with Spring Cloud 2020.0 从 spring-boot 中的 application.yml 文件中获取 cron - Get cron from application.yml file in spring-boot 在 spring-cloud 项目中,可以完全用 bootstrap.yml 替换 application.yml 吗? - In spring-cloud project, is it ok to replace application.yml with bootstrap.yml totally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM