Skip to content

Commit 6ac5953

Browse files
committed
Spring Boot 2.0 新特性:配置绑定 2.0 全解析
1 parent 9043420 commit 6ac5953

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

Chapter2-2-1/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter2-2-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter2-2-1</name>
12+
<description>Spring Boot 2 : Relaxed Binding</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
<version>1.16.20</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
6+
import org.springframework.boot.context.properties.bind.Bindable;
7+
import org.springframework.boot.context.properties.bind.Binder;
8+
import org.springframework.context.ApplicationContext;
9+
10+
import java.util.List;
11+
12+
/**
13+
*
14+
* @author 程序猿DD
15+
* @version 1.0.0
16+
* @blog http://blog.didispace.com
17+
*
18+
*/
19+
@SpringBootApplication
20+
public class Application {
21+
22+
public static void main(String[] args) {
23+
ApplicationContext context = SpringApplication.run(Application.class, args);
24+
25+
Binder binder = Binder.get(context.getEnvironment());
26+
27+
// 绑定简单配置
28+
FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
29+
System.out.println(foo.getFoo());
30+
31+
// 绑定List配置
32+
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
33+
System.out.println(post);
34+
35+
List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
36+
System.out.println(posts);
37+
38+
// 读取配置
39+
System.out.println(context.getEnvironment().containsProperty("com.didispace.database-platform"));
40+
System.out.println(context.getEnvironment().containsProperty("com.didispace.databasePlatform"));
41+
42+
}
43+
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties(prefix = "com.didispace")
8+
public class FooProperties {
9+
10+
private String foo;
11+
12+
private String databasePlatform;
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties
8+
public class PostInfo {
9+
10+
private String title;
11+
private String content;
12+
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
com.didispace.foo=bar
2+
com.didispace.database-platform=sql
3+
4+
com.didispace.post[0]=Why Spring Boot
5+
com.didispace.post[1]=Why Spring Cloud
6+
7+
com.didispace.posts[0].title=Why Spring Boot
8+
com.didispace.posts[0].content=It is perfect!
9+
com.didispace.posts[1].title=Why Spring Cloud
10+
com.didispace.posts[1].content=It is perfect too!

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
- [腾讯云:3年时长最低265元/年](https://cloud.tencent.com/redirect.php?redirect=1005&cps_key=f6a8af1297bfac40b9d10ffa1270029a)
1818
- [阿里云:ECS云服务器2折起](https://s.click.taobao.com/t?e=m%3D2%26s%3Dzj4kbQ5lKukcQipKwQzePCperVdZeJviEViQ0P1Vf2kguMN8XjClAq9GNeKfy2AD4SaRmc4YmqYCxNLxWxqxDPY8Eqzf%2BUWbOTauL6DcROffvu81lbXO1DDVuRn8ddiDsEVVC24eqozO54LQ%2FVw1L9X5LHh3Z8M%2BWS6ALZVeqlk9XUfbPSJC%2F06deTzTIbffYpyF7ku%2BxKgGargQjSAC4C6cUF%2FXAmem)
1919

20-
## 样例列表
20+
## Spring Boot 2.0 新特性学习
21+
22+
**简介与概览**
23+
24+
- [Spring Boot 2.0 正式发布,升还是不升呢?](http://blog.didispace.com/spring-boot-2-release/)
25+
- [Spring Boot 2.0 新特性和发展方向](http://blog.didispace.com/Spring-Boot-2-0-%E6%96%B0%E7%89%B9%E6%80%A7%E5%92%8C%E5%8F%91%E5%B1%95%E6%96%B9%E5%90%91/)
26+
- [Spring Boot 2.0 与 Java 9](http://blog.didispace.com/Spring-Boot-2.0%E4%B8%8EJava-9/)
27+
28+
**新特性详解**
29+
30+
- [Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析](http://localhost:4000/Spring-Boot-2-0-feature-1-relaxed-binding-2/)
31+
32+
## Spring Boot 基础教程(基于1.3.x-1.5.x)
2133

2234
#### 快速入门
2335

@@ -27,6 +39,7 @@
2739
#### 工程配置
2840

2941
- chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/)
42+
- chapter2-2-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/)
3043

3144
#### Web开发
3245

0 commit comments

Comments
 (0)