在Spring Boot应用中,读取配置通常通过application.properties
或application.yml
文件进行。不过,Spring Boot也支持从系统环境变量读取配置,这使得应用可以在不同的环境中灵活配置。下面详细介绍如何在Spring Boot中读取系统环境变量。
1. 配置优先级
Spring Boot有一套配置优先级机制,从多个来源读取配置,优先级从高到低依次是:
- 命令行参数
SPRING_APPLICATION_JSON
中的属性(系统环境变量或命令行参数)- Java系统属性(
System.getProperties()
) - 操作系统环境变量
application.properties
或application.yml
文件(位于src/main/resources
)- 通过
@PropertySource
注解声明的属性源 - 默认属性
2. 读取系统环境变量
在Spring Boot应用中,可以通过以下几种方式读取系统环境变量:
方法一:使用@Value
注解
可以直接在Spring组件中使用@Value
注解来注入系统环境变量:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Value("${MY_ENV_VARIABLE}")private String myEnvVariable;public void printEnvVariable() {System.out.println("MY_ENV_VARIABLE: " + myEnvVariable);}
}
假设环境变量MY_ENV_VARIABLE
的值是HelloWorld
,那么myEnvVariable
字段就会被注入HelloWorld
。
方法二:使用@ConfigurationProperties
注解
可以创建一个配置类来绑定多个环境变量:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Configuration
@ConfigurationProperties(prefix = "my.config")
public class MyConfig {private String envVariable;public String getEnvVariable() {return envVariable;}public void setEnvVariable(String envVariable) {this.envVariable = envVariable;}
}
假设有环境变量MY_CONFIG_ENV_VARIABLE
,那么在application.properties
中添加以下内容:
my.config.env-variable=${MY_CONFIG_ENV_VARIABLE}
然后可以在应用中使用MyConfig
类来获取环境变量的值。
方法三:使用Environment
对象
可以直接使用Spring的Environment
对象来访问环境变量:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Autowiredprivate Environment env;public void printEnvVariable() {String myEnvVariable = env.getProperty("MY_ENV_VARIABLE");System.out.println("MY_ENV_VARIABLE: " + myEnvVariable);}
}
方法四:命令行参数覆盖
可以在启动Spring Boot应用时通过命令行参数覆盖环境变量:
java -jar myapp.jar --MY_ENV_VARIABLE=HelloWorld
这种方式会覆盖环境变量,并且具有更高的优先级。
3. 示例
假设有一个Spring Boot项目,在读取环境变量时可以按照上述任一方式进行配置。以下是一个完整示例:
配置类
package com.example.demo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component
public class MyComponent {@Value("${MY_ENV_VARIABLE}")private String myEnvVariable;private final Environment env;public MyComponent(Environment env) {this.env = env;}@PostConstructpublic void printEnvVariables() {// 使用 @Value 注解读取System.out.println("MY_ENV_VARIABLE (via @Value): " + myEnvVariable);// 使用 Environment 对象读取String myEnvVariableViaEnv = env.getProperty("MY_ENV_VARIABLE");System.out.println("MY_ENV_VARIABLE (via Environment): " + myEnvVariableViaEnv);}
}
启动类
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
4. 运行应用
确保在操作系统中设置了环境变量MY_ENV_VARIABLE
,然后运行Spring Boot应用:
export MY_ENV_VARIABLE=HelloWorld
java -jar myapp.jar
应用启动后,会输出环境变量的值。
通过这些方法,可以灵活地在Spring Boot应用中读取和使用系统环境变量,使应用配置更加灵活和环境无关。