自定义属性及加载

经历了一个快速入门的环节,相信你已经大致了解了一些的基础内容,接下来我们将体验一些比较枯燥的内容。

本片内容叫做自定义属性及加载,

前面我们用到一个application.properties的配置文件,包括数据库的一些信息,我们都是在这里面配置的。

我们在这个文件里面添加这样几行代码。(我用的依然是2-5实战里的moment_demo)

1
2
3
4
#自定义属性
cn.surine.moment.name = Surine
cn.surine.moment.age = 22
cn.surine.moment.other = ${cn.surine.moment.name} is ${cn.surine.moment.age}

我们分别来看这几行。

第一行,cn.surine.moment可以随便写,我们叫做他prefix(前缀,注意不能写下划线,大写字母等特殊字符,会出现error)

name是属性名,后面的surine即属性的值

同理第二行,prefix + 属性名 。

第三行有所不同,不同之处,也就是属性值不同,${ xxx } 是一个占位,相信有编程基础的你可以理解,${cn.surine.moment.name}就是surine,而${cn.surine.moment.age}就是22,那other这个属性,值就是surine is 22。

所以我们来调用一下看看。

我们新建一个文件,这里可以没那么严格的包位置,我放在了Entities这个包里面。

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
package cn.surine.moment.Entities;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//把普通pojo实例化到spring容器中,记得使用属性调用的时候
//给类加上这个注解
@Component
public class PeopleEntity {

public String getOther() {
return other;
}

public void setOther(String other) {
this.other = other;
}

//取到name的时候,就会把属性文件中的name值surine传递过来了。
//格式如下:@Value("${prefix.属性名}")
@Value("${cn.surine.moment.name}")
private String name;

@Value("${cn.surine.moment.age}")
private int age;

@Value("${cn.surine.moment.other}")
private String other;

//get方法用于值的访问
public String getName() {
return name;
}

//set方法可以不写。
public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

最后,我在Controller里面添加了如下内容

1
2
3
4
5
6
@Autowired
private PeopleEntity peopleEntity;
@RequestMapping(value = "/getProperties")
public String getProperties(){
return peopleEntity.getName()+peopleEntity.getAge()+peopleEntity.getOther();
}

首先前两行,自动注入,很熟悉了,可以直接导入你要用的类。

下面的,也很熟悉,访问路径配置,然后方法体内部写返回的内容,是把我刚才定义的几个属性返回去了。

1548767275593

可以看到我访问对应路径已经出现效果了。说明我们的属性配置和访问没有问题

进一步探索

1
2
3
4
@RequestMapping(value = "/getRandom")
public String getRandom(){
return peopleEntity.getString();
}

我在Controller里面添加了这些内容,当然刚才我们已经注入了PeopleEntity,所以不需要再注入了。

可以看到我们调用了getString方法。

那么我们来看看,PeopleEntity里面加入了如下内容。

1
2
3
4
5
6
7
8
9
10
@Value("${cn.surine.moment.string}")
private String string;

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}

很正常,定义了属性和get/set方法,然后我们自然想到去看cn.surine.moment.string这个属性有什么不同。

1
2
# 随机字符串
cn.surine.moment.string=${random.value}

这个属性的作用是返回一串随机字符串,调用的方式是固定的,就写${random.value},场景也很常见,比如生成token啊之类的。

同样类似的属性还有

1
2
3
4
5
6
7
8
# 随机int
cn.surine.moment.int=${random.int}
# 随机long
cn.surine.moment.long=${random.long}
# 10以内的随机数
cn.surine.moment._10=${random.int(10)}
# 10-20的随机数
cn.surine.moment._10_20=${random.int[10,20]}

我们来看看效果。

1548767874216

继续探索

我们首先在pom中导入一个依赖,然后才能用接下来的方法来实现配置。

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

在属性文件中,添加:

1
2
3
cn.surine.moment.webName=Su
cn.surine.moment.webEmail=12345678@qq.com
cn.surine.moment.webPage=22

我们新定义了三个属性,name,email,page ,然后他们的prefix都是cn.surine.moment。

然后我在Entities里新建了一个WebConfig类,内容如下

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
package cn.surine.moment.Entities;

import org.springframework.boot.context.properties.ConfigurationProperties;

//我们这里直接使用ConfigurationProperties来加入前缀prefix,
//然后相关属性就不需要单独配置了。
@ConfigurationProperties(prefix = "cn.surine.moment")
public class WebConfig {
private String webName;
private String webEmail;
private int webPage;

public String getWebName() {
return webName;
}

public void setWebName(String webName) {
this.webName = webName;
}

public String getWebEmail() {
return webEmail;
}

public void setWebEmail(String webEmail) {
this.webEmail = webEmail;
}

public int getWebPage() {
return webPage;
}

public void setWebPage(int webPage) {
this.webPage = webPage;
}
}

接下来,我们给MomentDemoApplication中添加一句

1
@EnableConfigurationProperties(WebConfig.class)

把对应的类配置进去

1548768793664

最后Controller里面的内容就写

1
2
3
4
5
6
@Autowired
private WebConfig webConfig;
@RequestMapping(value = "/getWebConfig")
public WebConfig getWebConfig(){
return webConfig;
}

照例注入。

返回WebConfig对象。

测试结果如下。

1548768310819

对于自定义属性及配置可以将属性抽离,而且对于正式开发,属性不同的情况下比较方便修改。后面会有不同属性文件的介绍。

点我回到目录