【问题标题】:Reading MacOs Environment Variables in Spring Boot在 Spring Boot 中读取 MacOs 环境变量
【发布时间】:2020-05-05 11:04:46
【问题描述】:

我正在尝试使用环境变量在 Spring 启动属性文件“application.property”中设置一些 API 凭据。我的环境变量如下所示:

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

现在,当我运行 echo 命令时,我得到每个变量的“一些值”,甚至当我在常规 java 应用程序中运行 System.getenv("ACCOUNT_SID") 时。但是,当我在属性文件中设置值时,我得到的只是注入我的 POJO 字段的字符串“${ACCOUNT_SID}”和“${AUTH_TOKEN}”。即在我的 application.property 文件中假设以下配置:

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

其中 ACCOUNT_SID 和 AUTH_TOKEN 是我的环境变量。接下来我将我的 POJO 设置为:

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

    geters/setters
}

当我运行它并尝试打印字段的值时,我会在控制台中打印出 ${ACCOUNT_SID} 和 ${AUTH_TOKEN}。主 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);
    }
}

我什至尝试通过在变量上使用@Value 注释来注入值。当我使用 JAVA_HOME 环境变量进行测试时,我实际上要打印路径,但是当我尝试对我定义的环境变量执行相同操作时,程序崩溃了,因为找不到值。所以我必须给出一个默认值,即打印出来的值。下面的例子。

@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);
    }
}

请帮助我理解或看看我做错了什么。如果程序没有看到变量,让我认为我错误地设置了环境变量。但同时这让我感到困惑,因为当我在常规(不是 Spring Boot Java)程序中使用 System.getenv() 时,我可以打印这些值!我正在使用 Mac OS Catalina,我的环境变量存储在我的配置文件 ~/.profile 中。

【问题讨论】:

  • 在你的问题中,你说API_SID,但也说ACCOUNT_SID
  • 抱歉,这只是示例。它应该是 ACCOUNT_SID。我会尝试编辑它。
  • 不要将它们注入SpringBootApplication 类。创建一个不同的,相当确定这在生命周期中还为时过早,无法注入它们。您也错过了 TwilioConfig 课程的重点。将TwilioConfig 作为spring bean 注入并使用getters/setters/
  • 感谢您的反馈,我对 Spring 框架和 Spring Boot 非常陌生。

标签: java spring macos spring-boot environment-variables


【解决方案1】:

您可以在 ~/.bash_profile 上设置环境属性。 如果您使用 IntelliJ 运行您的应用程序,您可以通过打开“编辑运行/调试配置”对话框 -> 配置 -> 环境变量 -> 单击浏览 -> 检查包含系统环境变量来验证包含的系统属性(并验证是否你的变量在那里,如果不是你的环境属性不包括在内)

Verify in your Intellij Run/debug configuration

【讨论】:

    【解决方案2】:

    第一件事是你不应该调用SpringBootApplication类中的属性,但你应该从其他类型的上下文中使用@Value。除了使用@Valueapplication.properties 读取您的属性配置之外,您还应该使用相同的名称调用该属性,我查看了您的代码,但您没有调用相同的属性名称,例如:

    application.properties

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

    SmsHotLineApplication 类

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

    如果您想使用@ConfigurationProperties(prefix="twilio"),那么应该可以工作 然后你应该调用你命名属性的方式,例如:

    application.properties

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

    TwilioConfig 类

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

    【讨论】:

    • 当我像你建议的那样使用@Value 时,我的程序崩溃了,当我使用与 application.properties 文件中相同的名称时,程序仍会打印“${ACCOUNT_SID}”和“${AUTH_TOKEN} ”。我使用 @Value("${account.sid:DEFAULT}") 的原因是我使用 @Value("${account.sid:DEFAULT}") 的原因是直接访问我的环境变量,就像我使用 @Value("${java.home }") 其中 java.home 映射 JAVA_HOME 和 account.sid 映射 ACCOUNT_SID
    • 那些是错误的,当您在终端上设置的命令上使用export .... 时,您使用的是什么IDE?您应该将环境变量添加到您的操作系统中。这就是问题所在......
    • 我正在使用 Eclipse。我正在研究如何在 Mac OS 中设置环境变量,这就是我发现的。我认为这可能是问题所在。感谢您的链接。我去看看。
    • 是的,@LeoR 我更新了我的答案,您可以查看第一段,其中提到您应该在服务或其他类型的类上调用@Value,而不是在SpringBootApplicaton 类中使用。
    猜你喜欢
    • 2017-12-01
    • 2018-04-12
    • 2022-07-29
    • 2019-08-03
    • 2021-07-16
    • 2020-01-18
    • 2020-09-15
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多