spring获取Environment

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.16.RELEASE</version> </dependency>

package com.test.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; public class MyTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); Demo demo = context.getBean(Demo.class); Environment environment = demo.getEnvironment(); String java_home = environment.getProperty("JAVA_HOME"); System.out.println(java_home); } } @Configuration class MyConfig { @Bean public Demo demo() { return new Demo(); } } class Demo { @Autowired private Environment environment; public void setEnvironment(Environment environment) { this.environment = environment; } public Environment getEnvironment() { return environment; } }