简体   繁体   English

在 Spring Boot 中读取 MacOs 环境变量

[英]Reading MacOs Environment Variables in Spring Boot

I am trying to set some API credentials in the Spring boot property file "application.property" using environment variables.我正在尝试使用环境变量在 Spring 启动属性文件“application.property”中设置一些 API 凭据。 My environment variables look as such:我的环境变量如下所示:

export ACCOUNT_SID ="some value"
export AUTH_TOKEN="some value"

Now when I run echo command I get the "some value" for each variable and even when I run a System.getenv("ACCOUNT_SID") in a regular java application.现在,当我运行 echo 命令时,我会得到每个变量的“某个值”,甚至当我在常规 Java 应用程序中运行 System.getenv("ACCOUNT_SID") 时也是如此。 However, when I set the values in the property file, all I get is the strings "${ACCOUNT_SID}" and "${AUTH_TOKEN}" injected into my POJO fields.但是,当我在属性文件中设置值时,我得到的只是注入到我的 POJO 字段中的字符串“${ACCOUNT_SID}”和“${AUTH_TOKEN}”。 ie assume the following configuration in my application.property file:即假设我的 application.property 文件中有以下配置:

twilio.account_sid=${ACCOUNT_SID}
twilio.auth_token=${AUTH_TOKEN}

Where ACCOUNT_SID and AUTH_TOKEN are my environment variables.其中 ACCOUNT_SID 和 AUTH_TOKEN 是我的环境变量。 Next I set my POJO as such:接下来我将我的 POJO 设置为:

@Configuration
@ConfigurationProperties(prefix="twilio")
public class TwilioConfig {
    private String accountSid;
    private String authToken;

    geters/setters
}

When I run this and try to print the values of the fields I get ${ACCOUNT_SID} and ${AUTH_TOKEN} printed in the console.当我运行它并尝试打印字段的值时,我在控制台中打印了 ${ACCOUNT_SID} 和 ${AUTH_TOKEN}。 The main java file looks as such.主 java 文件看起来如此。

@SpringBootApplication
public class SmshotlineApplication implements CommandLineRunner {
    @Autowired
    private TwilioConfig twilio;

    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("ACCOUNT_SID="+twilio.getAccountSid());
        System.out.println("ACCOUNT_SID="+twilio.getAuthToken());
    }

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

在此处输入图片说明

I even try injecting the value by using @Value annotation on variables.我什至尝试通过在变量上使用 @Value 注释来注入值。 When I tested using JAVA_HOME environment variable, I actually got to print the path but when I try to do the same with my defined environment variables, the program crashed because the values could not be found.当我使用 JAVA_HOME 环境变量进行测试时,我实际上必须打印路径,但是当我尝试对定义的环境变量执行相同操作时,由于找不到值,程序崩溃了。 So I had to give a default value which is what gets printed.所以我不得不给出一个默认值,它是打印出来的。 example below.下面的例子。

@SpringBootApplication
public class SmshotlineApplication implements CommandLineRunner {

    @Value("${java.home}")
    String javaHome;
    @Value("${account.sid:DEFAULT}")
    String ACCOUNT_SID;
    @Value("${auth.token:DEFAULT}")
    String AUTH_TOKEN;

    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("ACCOUNT_SID="+ACCOUNT_SID);
        System.out.println("ACCOUNT_SID="+AUTH_TOKEN);
        System.out.println("JAVA_HOME="+javaHome);
    }


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

在此处输入图片说明

Please help me understand or see what am I doing wrong.请帮助我理解或看看我做错了什么。 If the variables are not being seen by the program, makes me think that I am setting the environment variables wrongly.如果程序没有看到这些变量,让我认为我错误地设置了环境变量。 But at the same time this confuses me because when I use System.getenv() in a regular(not spring boot Java) program, I get to print the values!但同时这让我感到困惑,因为当我在常规(不是 spring boot Java)程序中使用 System.getenv() 时,我可以打印值! I am using Mac OS Catalina and my env var are store in my profile file ~/.profile.我使用的是 Mac OS Catalina,我的 env var 存储在我的配置文件 ~/.profile 中。

You can set the environment properties on ~/.bash_profile.您可以在 ~/.bash_profile 上设置环境属性。 If you are running your app with IntelliJ you can verify the system properties that are included by opening 'Edit Run/Debug configuration' dialog -> Configuration -> Environment Variables -> Click on Browse -> Check Include System Environment Variables (and verify if your variables are in there, if not your environment properties were not included)如果您使用 IntelliJ 运行您的应用程序,您可以通过打开“编辑运行/调试配置”对话框 -> 配置 -> 环境变量 -> 单击浏览 -> 检查包含系统环境变量(并验证是否包含你的变量在那里,如果不是你的环境属性不包括在内)

Verify in your Intellij Run/debug configuration在您的 Intellij 运行/调试配置中进行验证

The first thing is you should not call the properties in SpringBootApplication class, but you should use @Value from other kind of context.第一件事是你不应该调用SpringBootApplication类中的属性,但你应该从其他类型的上下文中使用@Value In addition for reading your properties configs from application.properties by using @Value then you should call the property with the same name, I reviewed your code but you are not calling the same property name, for example:除了使用@Valueapplication.properties读取属性配置之外,您应该使用相同的名称调用属性,我查看了您的代码,但您没有调用相同的属性名称,例如:

application.properties应用程序属性

twilio.account_sid=${ACCOUNT_SID}
twilio.auth_token=${AUTH_TOKEN}

SmsHotLineApplication class SmsHotLineApplication 类

@Value("${twilio.account_sid:DEFAULT}")
String ACCOUNT_SID;
@Value("${twilio.auth_token:DEFAULT}")
String AUTH_TOKEN;

Then should work, if you want to use the @ConfigurationProperties(prefix="twilio") Then you should call the same how you named your properties, for example:然后应该可以工作,如果你想使用@ConfigurationProperties(prefix="twilio")那么你应该调用你命名属性的方式,例如:

application.properties应用程序属性

twilio.accountSid=${ACCOUNT_SID}
twilio.authToken=${AUTH_TOKEN}

TwilioConfig class TwilioConfig 类

@Configuration
@ConfigurationProperties(prefix="twilio")
public class TwilioConfig {
    private String accountSid;
    private String authToken;

    geters/setters
}

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

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