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!
|
||||
Reference in New Issue
Block a user