spring boot的默认配置application.properties文件
spring boot允许你自定义一个application.properties文件,然后放在以下的地方,来重写spring boot的环境变量或者定义你自己环境变量
- 当前目录的 “/config”的子目录下
- 当前目录下
- classpath根目录的“/config”包下
- classpath的根目录下
1.通过命令行来重写和配置环境变量,优先级最高,例如可以通过下面的命令来重写spring boot 内嵌tomcat的服务端口,注意“=”俩边不要有空格
1
|
java -jar demo.jar --server.port=9000 |
如果想要设置多个变量怎么办,可以已json的格式字符串来设置
1
|
java -jar demo.jar --spring.application.json= '{"foo":"bar"}'
|
2.通过@value注解来读取
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RestController @RequestMapping ( "/task" )
public
class TaskController {
@Value ( "${connection.remoteAddress}" )
private String address;
@RequestMapping (value = { "/" , "" })
public
String hellTask( @Value ( "${connection.username}" )String name){
return
"hello task !!" ;
} } |
3.通过Environment接口来获取,只需要把接口注进去即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@RestController @RequestMapping ( "/task" )
public
class TaskController {
@Autowired
Environment ev ;
@Value ( "${connection.remoteAddress}" )
private String address;
@RequestMapping (value = { "/" , "" })
public
String hellTask( @Value ( "${connection.username}" )String name){
String password = ev.getProperty( "connection.password" );
return
"hello task !!" ;
} } |
4.可以自定义一个工具类,来获取,这种方式关键在于读取配置文件信息,适合自定义的配置信息,spring 容器默认的配置信息会读不到
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
@Component public
class SystemConfig {
private
static Properties props ;
public
SystemConfig(){
try
{
Resource resource =
new ClassPathResource( "/application.properties" ); //
props = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取属性
* @param key
* @return
*/
public
static String getProperty(String key){
return
props == null
? null : props.getProperty(key);
}
/**
* 获取属性
* @param key 属性key
* @param defaultValue 属性value
* @return
*/
public
static String getProperty(String key,String defaultValue){
return
props == null
? null : props.getProperty(key, defaultValue);
}
/**
* 获取properyies属性
* @return
*/
public
static Properties getProperties(){
return
props;
}
} //用的话,就直接这样子 String value = SystemConfig.getProperty( "key" );
|
5.可以利用${…}在application.properties引用变量
1
2
|
myapp.name=spring myapp.desc=${myapp.name} nice |
6.可以在application.properties配置随机变量,利用的是RandomValuePropertySource类
1
2
3
4
5
|
my.secret=${random.value} my.number=${random. int }
my.bignumber=${random. long }
my.number.less.than.ten=${random. int ( 10 )}
my.number.in.range=${random. int [ 1024 , 65536 ]}
|