From 8be4255cbba970012eba8648349e6230a0e3ce42 Mon Sep 17 00:00:00 2001 From: Roman <32590063+oskar9247@users.noreply.github.com> Date: Wed, 11 Mar 2020 19:43:43 +0100 Subject: [PATCH] BAEL-3497 IntelliJ Cannot Resolve Spring Boot Configuration Properties (#8775) Co-authored-by: Oskar <> --- .../spring-boot-properties/pom.xml | 7 ++++ .../processor/CustomProperties.java | 37 +++++++++++++++++++ .../processor/DemoApplication.java | 12 ++++++ .../processor/PropertyBeanInjection.java | 22 +++++++++++ .../PropertyBeanInjectionUnitTest.java | 24 ++++++++++++ .../configuration-processor.properties | 2 + 6 files changed, 104 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/CustomProperties.java create mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/DemoApplication.java create mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties diff --git a/spring-boot-modules/spring-boot-properties/pom.xml b/spring-boot-modules/spring-boot-properties/pom.xml index 04879a5052..ec05ec1bdc 100644 --- a/spring-boot-modules/spring-boot-properties/pom.xml +++ b/spring-boot-modules/spring-boot-properties/pom.xml @@ -43,6 +43,12 @@ httpcore ${httpcore.version} + + org.springframework.boot + spring-boot-configuration-processor + ${configuration-processor.version} + true + @@ -121,6 +127,7 @@ 20.0 4.4.11 @ + 2.2.4.RELEASE diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/CustomProperties.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/CustomProperties.java new file mode 100644 index 0000000000..398b7557c4 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/CustomProperties.java @@ -0,0 +1,37 @@ +package com.baeldung.configuration.processor; + +import org.springframework.boot.context.properties.*; +import org.springframework.context.annotation.*; + +@Configuration +@ConfigurationProperties(prefix = "com.baeldung") +public class CustomProperties { + + /** + * The url to connect to. + */ + String url; + + /** + * The time to wait for the connection. + */ + private int timeoutInMilliSeconds = 1000; + + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public int getTimeoutInMilliSeconds() { + return timeoutInMilliSeconds; + } + + public void setTimeoutInMilliSeconds(int timeoutInMilliSeconds) { + this.timeoutInMilliSeconds = timeoutInMilliSeconds; + } + +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/DemoApplication.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/DemoApplication.java new file mode 100644 index 0000000000..1a35de86da --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/DemoApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.configuration.processor; + +import org.springframework.boot.*; +import org.springframework.boot.autoconfigure.*; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java new file mode 100644 index 0000000000..3bcbf41f54 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java @@ -0,0 +1,22 @@ +package com.baeldung.configuration.processor; + +import org.springframework.beans.factory.annotation.*; +import org.springframework.stereotype.*; + +@Component +public class PropertyBeanInjection { + + private final CustomProperties customProperties; + + PropertyBeanInjection(@Autowired CustomProperties customProperties) { + this.customProperties = customProperties; + } + + String getUrl() { + return customProperties.getUrl(); + } + + int getTimeoutInMilliseconds() { + return customProperties.getTimeoutInMilliSeconds(); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java new file mode 100644 index 0000000000..bdeb6547c3 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.configuration.processor; + +import org.junit.jupiter.api.*; +import org.junit.runner.*; +import org.springframework.beans.factory.annotation.*; +import org.springframework.boot.test.context.*; +import org.springframework.test.context.*; +import org.springframework.test.context.junit4.*; + +@RunWith(SpringRunner.class) +@TestPropertySource("/configuration-processor.properties") +@SpringBootTest(classes = DemoApplication.class) +class PropertyBeanInjectionUnitTest { + + @Autowired + private PropertyBeanInjection propertyBeanInjection; + + @Test + void checkThatCustomPropertiesHaveTheCorrectValueFromPropertiesFile() { + Assertions.assertEquals("www.abc.test.com", propertyBeanInjection.getUrl()); + Assertions.assertEquals(2000, propertyBeanInjection.getTimeoutInMilliseconds()); + } + +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties b/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties new file mode 100644 index 0000000000..00369f2eff --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties @@ -0,0 +1,2 @@ +com.baeldung.url=www.abc.test.com +com.baeldung.timeout-in-milli-seconds=2000