【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量
Spring boot 读取系统环境变量
凡是被 Spring 管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到
系统环境变量和 application 配置文件中的变量。
com.hpit.sb.environment.MyEnvironment
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* TODO 读取spring以及系统环境变量 主要是@Configuration,
实现接口:EnvironmentAware就能获取到系统
环境信息;
*
*
*/
@Configuration
public class MyEnvironment implements EnvironmentAware {
@Value("${spring.datasource.url}") // 使用el表达式读取spring主配置文件
private String jdbcUrl;
private Logger logger = Logger.getLogger(getClass());
@Override
public void setEnvironment(Environment environment) {
// springEL表达式获取的值
logger.info("springel表达式获取的值:
" + jdbcUrl);
// 获取系统属性:
logger.info("JAVA_HOME" + environment.getProperty("JAVA_HOME"));
// 获取spring主配置文件中的属性
logger.info("spring.datasource.url:" + environment.getProperty("spring.datasource.url"));
// 获取前缀是“spring.datasource”的所有属性值
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment,
"spring.datasource.");
logger.info("通过前缀获取的url:" + propertyResolver.getProperty("url"));
logger.info("通过前缀获取的driverClassName:" + propertyResolver.getProperty("driverClassName"));
}
}
其中 application.properties 文件信息是:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
启动应用,查看日志输出:
@Controller @Service 等被 Spring 管理的类都支持,注意重写的方法 setEnvironment 是在系统启动的时候被
执行。
或者如下 Controller:
com.hpit.sb.controller.SystemEnvironmentController
import org.apache.log4j.Logger;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* TODO 在普通的控制器和Servie业务中也可以直接实现EnvironmentAware来获取系统环境变量,但是在获取系
统环境变量的时机为系统加载的时候
*
*
*/
@RestController
@RequestMapping("/system")
public class SystemEnvironmentController implements EnvironmentAware {
private String java_home;
private Logger logger = Logger.getLogger(getClass());
@RequestMapping("/javahome")
public String getJAVAHOME() {
return java_home;
}
@Override
public void setEnvironment(Environment environment) {
java_home = environment.getProperty("JAVA_HOME");
logger.info("控制器中获取的系统环境变量:" + java_home);
}
}
请求控制器:查看效果

日志输出:

我们还可以通过@ConfigurationProperties 读取 application 属性配置文件中的属性。
具体代码:
com.hpit.sb.config.MyDataConfiguration
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MySqlConfig.class)
public class MyDataConfiguration {
@Autowired
private MySqlConfig config;
}
com.hpit.sb.config.MySqlConfig
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="spring.datasource.")
public class MySqlConfig {
private String url;
private String username;
private String password;
private String driverClassName;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
代码解释:
@ConditionOnClass 表明该@Configuration 仅仅在一定条件下才会被加载,这里的条件是 Mongo.class 位于类
路径上
· @EnableConfigurationProperties 将 Spring Boot 的配置文件( application.properties )中的
spring.data.mongodb.*属性映射为 MongoProperties 并注入到 MongoAutoConfiguration 中。
· @ConditionalOnMissingBean 说明 Spring Boot 仅仅在当前上下文中不存在对象时,才会实例化一个
Bean。这个逻辑也体现了 Spring Boot 的另外一个特性——自定义的 Bean 优先于框架的默认配置,我们如果显
式的在业务代码中定义了一个对象,那么 Spring Boot 就不再创建。
【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量【Spring boot 读取系统环境变量】 Spring boot 读取系统环境变量