环境变量
凡是被spring管理的类,实现接口EnvironmentAware 重写方法 setEnvironment
可以在工程启动时,获取到系统环境变量和application配置文件中的变量。
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;
@Configuration
public class CustomEnviroment implements EnvironmentAware{
@Value("${spring.datasource.url}")
private String datasourceUrl;
/**
* 注意重写的方法 setEnvironment 是在系统启动的时候被执行
*/
@Override
public void setEnvironment(Environment environment){
//打印注入的属性信息.
System.out.println("datasourceUrl="+datasourceUrl);
//通过 environment 获取到系统属性.
System.out.println(environment.getProperty("JAVA_HOME"));
//通过 environment 同样能获取到application.properties配置的属性.
System.out.println(environment.getProperty("spring.datasource.url"));
//获取到前缀是"spring.datasource." 的属性列表值.
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(environment,"spring.datasource.");
System.out.println("spring.datasource.url="+relaxedPropertyResolver.getProperty("url"));
System.out.println("spring.datasource.driverClassName="+relaxedPropertyResolver.getProperty("driverClassName"));
}
}
//输出
datasourceUrl=jdbc:mysql://172.16.24.47:3306/test_s
D:\Program Files\Java\jdk1.7.0_79
jdbc:mysql://172.16.24.47:3306/test_s
spring.datasource.url=jdbc:mysql://172.16.24.47:3306/test_s
spring.datasource.driverClassName=com.mysql.jdbc.Driver
属性对象绑定
pom依赖
<!--spring-boot-configuration:springboot 配置处理器; -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
说明:通过使用spring-boot-configuration-processorjar, 你可以从被@ConfigurationProperties注解的节点轻松的产生自己的配置元数据文件。
该jar包含一个在你的项目编译时会被调用的Java注解处理器。想要使用该处理器,你只需简单添加spring-boot-configuration-processor依赖。
注解
@ConfigurationProperties 读取application属性配置文件中的属性
application.properties配置
tutorial.name=jack
tutorial.password=1234
代码
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="tutorial")
//@Component如果这里使用了此注解,则别处不在需要使用EnableConfigurationProperties注解了
public class MysqlProperties{
//通过名字直接映射为对象的属性
private String url="localhost";//不配置,默认为localhost
private String name;
private String password;
public String getUrl(){
return url;
}
public void setUrl(String url){
this.url = url;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
@Configuration
@EnableConfigurationProperties(MysqlProperties.class)
public class MysqlConfiguration{
@Autowired
private MysqlProperties mysqlProperties;
}
如果新建一个新的properties文件,则如下获取
custom.properties自定义文件
test.name=lucy
test.password=233
test.param=name:${test.name}and password is${test.password}
test.ranstr=${random.value}
test.ranint=${random.int}
test.ranlong=${random.long}
test.ranint1=${random.int(10)}
test.ranint2=${random.int[10,20]}
//注意路径
@ConfigurationProperties(prefix = "test",locations = "classpath:custom.properties")
public class CustomProperties{
private String name;
private String password;
private String param;//参数化
private String ranstr;//随机字符串
private String ranint;//随机int
private String ranlong;//随机long
private String ranint1;//随机int10以内
private String ranint2;//随机int10-20
//getter、setter方法
如上写一个配置类或者在启动类上加一个注解@EnableConfigurationProperties
@SpringBootApplication
@ServletComponentScan
@EnableConfigurationProperties(CustomProperties.class)
public class AppStart{
public static void main(String[] args){
SpringApplication.run(AppStart.class,new String[]{"hello","world"});
}
}