From 48a8f0dc504f5b483f2bbd5a4fc3db6a42b6f804 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Fri, 17 Jul 2020 02:05:13 +0800 Subject: [PATCH] test yml map --- .../boot/sample/beans/TestProperties.java | 16 ++++++++++++++++ .../autoconfigure/ApplicationConfiguration.java | 8 ++++++-- .../src/main/resources/application.yml | 7 ++++++- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/TestProperties.java diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/TestProperties.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/TestProperties.java new file mode 100644 index 00000000..299de35b --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/TestProperties.java @@ -0,0 +1,16 @@ +package org.hongxi.whatsmars.boot.sample.beans; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Map; + +/** + * Created by shenhongxi on 2020/7/17. + */ +@Data +@ConfigurationProperties(prefix = "test") +public class TestProperties { + + private Map map; +} diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/ApplicationConfiguration.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/ApplicationConfiguration.java index 5e6373d4..cd52b193 100644 --- a/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/ApplicationConfiguration.java +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/ApplicationConfiguration.java @@ -7,6 +7,7 @@ import org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegis import org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegistryPostProcessor2; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -20,12 +21,14 @@ import java.util.List; * Created by shenhongxi on 2020/6/22. */ @Configuration +@EnableConfigurationProperties(TestProperties.class) public class ApplicationConfiguration { @Order(1) @ConditionalOnMissingBean @Bean - public DemoBean demoBean() { + public DemoBean demoBean(TestProperties testProperties) { + System.out.println(testProperties.getMap()); return new DemoBean(); } @@ -62,7 +65,7 @@ public class ApplicationConfiguration { return new PlanetBeanDefinitionRegistryPostProcessor2(parseNames(environment)); } - public List parseNames(Environment environment) { + private List parseNames(Environment environment) { List names = new ArrayList<>(); String configsKey = "planet.names[%d]"; int configIndex = 0; @@ -72,4 +75,5 @@ public class ApplicationConfiguration { } return names; } + } diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml index c29c4b00..0fa9f31f 100644 --- a/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml @@ -3,4 +3,9 @@ a: xx planet: names: - Mars - - Earth \ No newline at end of file + - Earth + +test: + map: + first: 1 + second: 2 \ No newline at end of file -- GitLab