简体   繁体   English

启动 Micronaut 应用程序时如何打印环境变量

[英]How to print out environment variables, when starting a Micronaut application

I am working on a Micronaut project, where I would like to see if the environment variables from the application.yml are being correctly assigned using the @Value annotation, when the app starts locally.我正在开发一个 Micronaut 项目,我想查看应用程序在本地启动时是否使用 @Value 注释正确分配了 application.yml 中的环境变量。 But every time the app is starting it shows me that the variables are not being assigned to the environment variables from the application.yml file.但是每次应用程序启动时,它都会告诉我变量没有从 application.yml 文件分配给环境变量。

That is my code:那是我的代码:

public class Application {

    private static String localTestString = "I am the local String";

    @Value("${aws.secretkeyid}")
    public static String applicationYmlTestString;

    @Value("${aws.keyid}")
    private static int keyId;

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

    static{
        log.warn("Local Test String is: " + localTestString);
        log.warn("Application Yml Test String is: " + applicationYmlTestString);
        log.warn("Key ID: " + keyId);
    }

}

This is my application.yml这是我的 application.yml

aws:
  keyid: 123
  secretkeyid: "abcdesdasdsddddd"
  region: "europe-1"

Output: Output:

Local Test String is: I am the local String
Application Yml Test String is: null
Key ID: 0

As we see the two variables applicationYmlTestString and keyId are not being assigned to the environment variables.正如我们看到的两个变量applicationYmlTestStringkeyId没有被分配给环境变量。 Is there a way to solve this problem and to get:有没有办法解决这个问题并获得:

Application Yml Test String is: abcdesdasdsddddd
Key ID: 123

Thank you in advance!先感谢您!

There are two issues with the example you have shown.您展示的示例有两个问题。 Firstly, Micronaut does not inject values to static fields annotated with @Value annotation.首先,Micronaut不会将值注入到带有@Value注释的 static 字段中。 (It's not weird, Spring does not support it as well .) Secondly, after injecting values to non-static fields, you won't be able to read their values using the class' static constructor. (这并不奇怪, Spring 也不支持它。)其次,在将值注入非静态字段后,您将无法使用类的 static 构造函数读取它们的值。 The whole application context must be ready to read such values, so you need to use an event listener that reacts to the application startup event.整个应用程序上下文必须准备好读取这些值,因此您需要使用对应用程序启动事件作出反应的事件侦听器。

Here is the simplest way to achieve it based on your example:这是根据您的示例实现它的最简单方法:

package micronaut.hello.world;

import io.micronaut.context.annotation.Value;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.Micronaut;
import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    @Value("${aws.secretkeyid}")
    private String applicationYmlTestString;

    @Value("${aws.keyid}")
    private int keyId;

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

    @EventListener
    void onStartup(StartupEvent event) {
        log.warn("Application Yml Test String is: " + applicationYmlTestString);
        log.warn("Key ID: " + keyId);
    }
}

There are three things worth mentioning:有三点值得一提:

  1. The above example uses @EventListener annotation that makes the given method "event-aware", and this method will be triggered when the specific event is published by the application (or framework.)上面的例子使用@EventListener注解,使得给定的方法“事件感知”,当特定的事件被应用程序(或框架)发布时,这个方法将被触发。
  2. We react to io.micronaut.context.event.StartupEvent - an event fired once startup is complete.我们对io.micronaut.context.event.StartupEvent做出反应 - 启动完成后触发的事件。
  3. Keep in mind that to make this @EventListener annotation work, we need to annotate the application class with @Singleton to make this class a proper Micronaut bean.请记住,要使此@EventListener注释工作,我们需要使用 @Singleton 注释应用程序@Singleton以使此 class 成为适当的 Micronaut bean。

Alternatively, if making an application class a singleton bean does not look good to you, you can implement the ApplicationEventListener interface and create a dedicated bean that will react to the same startup event.或者,如果让应用程序 class 成为 singleton bean 对您来说看起来不太好,您可以实现ApplicationEventListener接口并创建一个专用 bean 来响应相同的启动事件。 In this example, I use a static inner class, but that's just to make this example simple:在此示例中,我使用 static 内部 class,但这只是为了简化此示例:

package micronaut.hello.world;

import io.micronaut.context.annotation.Value;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.Micronaut;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

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

    @Singleton
    static class OnStartupEventListener implements ApplicationEventListener<StartupEvent> {

        @Value("${aws.secretkeyid}")
        private String applicationYmlTestString;

        @Value("${aws.keyid}")
        private int keyId;

        @Override
        public void onApplicationEvent(StartupEvent event) {
            log.warn("Application Yml Test String is: " + applicationYmlTestString);
            log.warn("Key ID: " + keyId);
        }
    }
}

But eventually, you should consider implementing a configuration class and use it instead of injecting values with the @Value annotation.但最终,您应该考虑实现配置 class 并使用它而不是使用@Value注释注入值。 However, whatever option you choose, the same thing applies - the configuration class can be injected to a non-static field and can be checked using an event listener mechanism.但是,无论您选择什么选项,同样的事情适用 - 配置 class 可以注入到非静态字段,并且可以使用事件侦听器机制进行检查。

And as Tim mentioned in the comment below, "Be careful logging environment variables though... They have a habit of being secrets, and logging them out tends to end up with them being in plain text in loads of different systems " .正如蒂姆在下面的评论中提到的那样, “但要小心记录环境变量......它们有保密的习惯,并且将它们注销往往最终导致它们在不同系统的负载中以纯文本形式出现” If you really need to log such information to double-check if the expected configuration is injected, try doing it in the controlled dev environment only.如果您确实需要记录此类信息以仔细检查是否注入了预期的配置,请尝试仅在受控的开发环境中进行。 Assuming that you use the dev profile for the local env, you could use @Requires annotation to limit specific event listener to only that dev environment:假设您使用本地环境的dev配置文件,您可以使用@Requires注释将特定事件侦听器限制为仅该dev环境:

@Singleton
@Requires(env = "dev")
class OnStartupEventListener implements ApplicationEventListener<StartupEvent> {

    @Value("${aws.secretkeyid}")
    private String applicationYmlTestString;

    @Value("${aws.keyid}")
    private int keyId;

    @Override
    public void onApplicationEvent(StartupEvent event) {
        log.warn("Application Yml Test String is: " + applicationYmlTestString);
        log.warn("Key ID: " + keyId);
    }
}

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

相关问题 Micronaut + Maven - 环境变量 - Micronaut + Maven - Environment variables 启动Maveryx测试应用程序时如何指定环境变量? - How can environment variables be specified when launching a Maveryx test application? 如何在 Micronaut 应用程序中指定配置文件? - How to specify a profile in a Micronaut application? 尝试运行应用程序时 Micronaut NonUniqueBeanException - Micronaut NonUniqueBeanException when attempting to run application 在 micronaut 应用程序中访问 application.yml 中的环境变量 - Access environment variable in application.yml in a micronaut app 如何使用Micronaut运行KafkaStream应用程序? - How can I run KafkaStream application with Micronaut? 如何在 micronaut 中为应用程序设置根日志级别 - How to set root log level for application in micronaut 如何将属性源分配给 micronaut 中的某个环境? - How do I assign a property source to a certain environment in micronaut? 启动Tomcat时出错,不支持的主要/次要版本,环境变量似乎正确 - Error when starting Tomcat, unsupported major/minor version, environment variables seem correct though 如何使用从文件加载的环境变量运行java应用程序? - how to run java application with environment variables loading from file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM