Spring Boot 2.0 新特性:配置绑定 2.0 全解析
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter2-2-1</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter2-2-1</name>
|
||||
<description>Spring Boot 2 : Relaxed Binding</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.20</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -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<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
|
||||
System.out.println(post);
|
||||
|
||||
List<PostInfo> 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"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
+10
@@ -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!
|
||||
@@ -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开发
|
||||
|
||||
|
||||
Reference in New Issue
Block a user