Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
2018-01-30 14:29
1001 查看
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以
错误的.不能读取的例子:
mySet .ABAP_AS_POOLED = ABAP_AS_WITH_POOL
不要带下划线,正确的例子
mySet.ABAPASPOOLED = ABAP_AS_WITH_POOL
(下划线的坑,坑了我两天..特此纪念)
读取核心配置文件
核心配置文件是指在resources根目录下的
核心配置文件
1
2
3
使用
1
2
3
4
5
6
7
8
9
10
11
注意:在
访问:http://localhost:9090/index 时将得到
使用
1
2
3
4
5
6
7
8
9
10
11
12
注意:这种方式是依赖注入
访问:http://localhost:9090/index2 时将得到
读取自定义配置文件
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在
1
2
3
创建管理配置的实体类:
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
注意:
在
使用
创建测试Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
注意:由于在MyWebConfig类上加了注释
访问:http://localhost:9090/config/index 时将得到
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study02
本文章来自【知识林】
版权声明:本文为博主原创文章,未经博主允许不得转载。更多资料上【知识林】:www.zslin.com
错误的.不能读取的例子:
mySet .ABAP_AS_POOLED = ABAP_AS_WITH_POOL
不要带下划线,正确的例子
mySet.ABAPASPOOLED = ABAP_AS_WITH_POOL
(下划线的坑,坑了我两天..特此纪念)
读取核心配置文件
核心配置文件是指在resources根目录下的
application.properties或
application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件
application.properties内容如下:
server.port=9090 test.msg=Hello World Springboot!
1
2
3
使用
@Value方式(常用):
@RestController public class WebController { @Value("${test.msg}") private String msg; @RequestMapping(value = "index", method = RequestMethod.GET) public String index() { return "The Way 1 : " +msg; } }
1
2
3
4
5
6
7
8
9
10
11
注意:在
@Value的${}中包含的是核心配置文件中的键名。在Controller类上加
@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加
@ResponseBody。
访问:http://localhost:9090/index 时将得到
The Way 1 : Hello World Springboot!
使用
Environment方式
@RestController public class WebController { @Autowired private Environment env; @RequestMapping(value = "index2", method = RequestMethod.GET) public String index2() { return "The Way 2 : " + env.getProperty("test.msg"); } }
1
2
3
4
5
6
7
8
9
10
11
12
注意:这种方式是依赖注入
Evnironment来完成,在创建的成员变量
private Environment env上加上
@Autowired注解即可完成依赖注入,然后使用
env.getProperty("键名")即可读取出对应的值。
访问:http://localhost:9090/index2 时将得到
The Way 2 : Hello World Springboot!
读取自定义配置文件
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在
resources/config目录下创建配置文件
my-web.properties
resources/config/my-web.properties内容如下:
web.name=zslin web.version=V 1.0 web.author=393156105@qq.com
1
2
3
创建管理配置的实体类:
@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web") @Component public class MyWebConfig { private String name; private String version; private String author; public String getAuthor() { return author; } public String getName() { return name; } public String getVersion() { return version; } public void setAuthor(String author) { this.author = author; } public void setName(String name) { this.name = name; } public void setVersion(String version) { this.version = version; } }
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
注意:
在
@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以
web.开头)
使用
@Component是让该类能够在其他地方被依赖使用,即使用
@Autowired注释来创建实例。
创建测试Controller
@RestController @RequestMapping(value = "config") public class ConfigController { @Autowired private MyWebConfig myWebConfig; @RequestMapping(value = "index", method = RequestMethod.GET) public String index() { return "webName: "+myWebConfig.getName()+", webVersion: "+ myWebConfig.getVersion()+", webAuthor: "+myWebConfig.getAuthor(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
注意:由于在MyWebConfig类上加了注释
@Component,所以可以直接在这里使用
@Autowired来创建其实例对象。
访问:http://localhost:9090/config/index 时将得到
webName: zslin, webVersion: V 1.0, webAuthor: 393156105@qq.com
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study02
本文章来自【知识林】
版权声明:本文为博主原创文章,未经博主允许不得转载。更多资料上【知识林】:www.zslin.com
相关文章推荐
- springboot中从配置文件读取字符串在控制台输出出现乱码,为什么?
- Springboot 之 自定义配置文件及读取配置文件
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- Springboot 之 自定义配置文件及读取配置文件
- Spring-boot中读取核心配置文件application和自定义properties配置文件的方式
- Spring boot 读取配置文件——@Value的使用
- spring boot 添加自定义配置文件并读取属性
- Springboot 之 自定义配置文件及读取配置文件
- Spring boot之读取自定义配置文件
- 五、Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- SpringBoot自定义配置文件读取
- spring boot 读取配置文件问题,不能使用@Autowired
- Springboot读取配置文件及自定义配置文件的方法
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 读取配置文件及自定义配置文件
- spring-boot读取props和yml配置文件
- spring boot 单元测试与读取配置文件(application.yml)中的属性值
- Spring-boot中读取config配置文件的两种方式