diff --git a/Chapter2-2-1/pom.xml b/Chapter2-2-1/pom.xml new file mode 100644 index 00000000..c7eddd26 --- /dev/null +++ b/Chapter2-2-1/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.didispace + Chapter2-2-1 + 1.0.0 + jar + + Chapter2-2-1 + Spring Boot 2 : Relaxed Binding + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + 1.16.20 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter2-2-1/src/main/java/com/didispace/Application.java b/Chapter2-2-1/src/main/java/com/didispace/Application.java new file mode 100755 index 00000000..f06a4a25 --- /dev/null +++ b/Chapter2-2-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,44 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.context.ApplicationContext; + +import java.util.List; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + ApplicationContext context = SpringApplication.run(Application.class, args); + + Binder binder = Binder.get(context.getEnvironment()); + + // 绑定简单配置 + FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get(); + System.out.println(foo.getFoo()); + + // 绑定List配置 + List post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get(); + System.out.println(post); + + List posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get(); + System.out.println(posts); + + // 读取配置 + System.out.println(context.getEnvironment().containsProperty("com.didispace.database-platform")); + System.out.println(context.getEnvironment().containsProperty("com.didispace.databasePlatform")); + + } + +} diff --git a/Chapter2-2-1/src/main/java/com/didispace/FooProperties.java b/Chapter2-2-1/src/main/java/com/didispace/FooProperties.java new file mode 100644 index 00000000..9fdf6e22 --- /dev/null +++ b/Chapter2-2-1/src/main/java/com/didispace/FooProperties.java @@ -0,0 +1,14 @@ +package com.didispace; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Data +@ConfigurationProperties(prefix = "com.didispace") +public class FooProperties { + + private String foo; + + private String databasePlatform; + +} \ No newline at end of file diff --git a/Chapter2-2-1/src/main/java/com/didispace/PostInfo.java b/Chapter2-2-1/src/main/java/com/didispace/PostInfo.java new file mode 100644 index 00000000..6df554fe --- /dev/null +++ b/Chapter2-2-1/src/main/java/com/didispace/PostInfo.java @@ -0,0 +1,13 @@ +package com.didispace; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Data +@ConfigurationProperties +public class PostInfo { + + private String title; + private String content; + +} diff --git a/Chapter2-2-1/src/main/resources/application.properties b/Chapter2-2-1/src/main/resources/application.properties new file mode 100755 index 00000000..df1d5f5a --- /dev/null +++ b/Chapter2-2-1/src/main/resources/application.properties @@ -0,0 +1,10 @@ +com.didispace.foo=bar +com.didispace.database-platform=sql + +com.didispace.post[0]=Why Spring Boot +com.didispace.post[1]=Why Spring Cloud + +com.didispace.posts[0].title=Why Spring Boot +com.didispace.posts[0].content=It is perfect! +com.didispace.posts[1].title=Why Spring Cloud +com.didispace.posts[1].content=It is perfect too! diff --git a/README.md b/README.md index 796bbec7..09a7ef9a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,19 @@ - [腾讯云:3年时长最低265元/年](https://cloud.tencent.com/redirect.php?redirect=1005&cps_key=f6a8af1297bfac40b9d10ffa1270029a) - [阿里云:ECS云服务器2折起](https://s.click.taobao.com/t?e=m%3D2%26s%3Dzj4kbQ5lKukcQipKwQzePCperVdZeJviEViQ0P1Vf2kguMN8XjClAq9GNeKfy2AD4SaRmc4YmqYCxNLxWxqxDPY8Eqzf%2BUWbOTauL6DcROffvu81lbXO1DDVuRn8ddiDsEVVC24eqozO54LQ%2FVw1L9X5LHh3Z8M%2BWS6ALZVeqlk9XUfbPSJC%2F06deTzTIbffYpyF7ku%2BxKgGargQjSAC4C6cUF%2FXAmem) -## 样例列表 +## Spring Boot 2.0 新特性学习 + +**简介与概览** + +- [Spring Boot 2.0 正式发布,升还是不升呢?](http://blog.didispace.com/spring-boot-2-release/) +- [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/) +- [Spring Boot 2.0 与 Java 9](http://blog.didispace.com/Spring-Boot-2.0%E4%B8%8EJava-9/) + +**新特性详解** + +- [Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析](http://localhost:4000/Spring-Boot-2-0-feature-1-relaxed-binding-2/) + +## Spring Boot 基础教程(基于1.3.x-1.5.x) #### 快速入门 @@ -27,6 +39,7 @@ #### 工程配置 - chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/) +- chapter2-2-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/) #### Web开发