diff --git a/pom.xml b/pom.xml index 43f31d5c98..b20d82618b 100644 --- a/pom.xml +++ b/pom.xml @@ -226,6 +226,7 @@ spring-drools drools liquibase + spring-boot-property-exp diff --git a/spring-boot-property-exp/README.md b/spring-boot-property-exp/README.md new file mode 100644 index 0000000000..5b2552ade7 --- /dev/null +++ b/spring-boot-property-exp/README.md @@ -0,0 +1,2 @@ +## The Module Holds Sources for the Following Articles + - [Automatic Property Expansion with Spring Boot] (http://www.baeldung.com/property-expansion-spring-boot) \ No newline at end of file diff --git a/spring-boot-property-exp/pom.xml b/spring-boot-property-exp/pom.xml new file mode 100644 index 0000000000..f554bb5b99 --- /dev/null +++ b/spring-boot-property-exp/pom.xml @@ -0,0 +1,32 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + 4.0.0 + + com.baeldung + spring-boot-property-exp + 0.0.1-SNAPSHOT + + pom + + spring-boot-property-exp + http://maven.apache.org + + + UTF-8 + + + + property-exp-default + property-exp-custom + + + + diff --git a/spring-boot-property-exp/property-exp-custom/pom.xml b/spring-boot-property-exp/property-exp-custom/pom.xml new file mode 100644 index 0000000000..234404a6c0 --- /dev/null +++ b/spring-boot-property-exp/property-exp-custom/pom.xml @@ -0,0 +1,60 @@ + + + spring-boot-property-exp + com.baeldung + 0.0.1-SNAPSHOT + + 4.0.0 + + com.baeldung + property-exp-custom + 0.0.1-SNAPSHOT + jar + + property-exp-custom + http://maven.apache.org + + + UTF-8 + Custom Property Value + + + + + org.springframework.boot + spring-boot-starter + 1.5.4.RELEASE + + + + + + + ${basedir}/src/main/resources + true + + **/application*.yml + **/application*.yaml + **/application*.properties + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + + @ + + true + + + + + + + diff --git a/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java b/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java new file mode 100644 index 0000000000..d88dbf0123 --- /dev/null +++ b/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java @@ -0,0 +1,13 @@ +package com.baeldung.propertyexpansion; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootPropertyExpansionApp { + + public static void main(String[] args) { + SpringApplication.run(SpringBootPropertyExpansionApp.class, args); + } + +} diff --git a/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java b/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java new file mode 100644 index 0000000000..920996e120 --- /dev/null +++ b/spring-boot-property-exp/property-exp-custom/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java @@ -0,0 +1,37 @@ +package com.baeldung.propertyexpansion.components; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class PropertyLoggerBean { + + private static final Logger log = LoggerFactory.getLogger(PropertyLoggerBean.class); + + @Value("${expanded.project.version}") + private String projectVersion; + + @Value("${expanded.project.property}") + private String projectProperty; + + + @PostConstruct + public void printProperties() { + + log.info(""); + log.info("Properties logged in a bean:"); + log.info("==========================================="); + log.info("Project version : {}", projectVersion); + log.info("Project property : {}", projectProperty); + log.info("==========================================="); + log.info(""); + + } + + + +} diff --git a/spring-boot-property-exp/property-exp-custom/src/main/resources/application.properties b/spring-boot-property-exp/property-exp-custom/src/main/resources/application.properties new file mode 100644 index 0000000000..545fcc2e46 --- /dev/null +++ b/spring-boot-property-exp/property-exp-custom/src/main/resources/application.properties @@ -0,0 +1,2 @@ +expanded.project.version=${project.version} +expanded.project.property=${custom.property} \ No newline at end of file diff --git a/spring-boot-property-exp/property-exp-custom/src/main/resources/banner.txt b/spring-boot-property-exp/property-exp-custom/src/main/resources/banner.txt new file mode 100644 index 0000000000..2b5d7f1ee5 --- /dev/null +++ b/spring-boot-property-exp/property-exp-custom/src/main/resources/banner.txt @@ -0,0 +1,21 @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@#@@@@@########@@@@@@@@@@@@@@@@@@@@@@@@...@@@@@@@@@:..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#. @@@@@* *@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@#o @@@@@* @@@@@* @@@:*.*@@@@@@@: *8@@@ @@@@&:.#@. @o**@@@@**:@o*o@@:.:@@@@@:.o#@&*:@@@@ +@@@@@@@@@@@@* @@@@@* 8888 8@ @@@8 #@o 8@# .@ @@* :. @* @@@@ @. : &@ ** .@@@@ +@@@@@@@@@@. @ o@@@@@* *@@@o::& .* 8@@@@. @@ 8@@@@. @* @@@@ @. @@@& * @@@@# .@@@@ +@@@@@@@@@& @ @@@@@@* @@@@@@ 8 @@@@ .. o&&&&&&& @@ #@@@@. @* @@@@ @. @@@# * @@@@@ .@@@@ +@@@@@@@@@ @@o @@@@@@@* oooo* 8 @@@& @* @@@ # 88. 88. *& o#: @. @@@# *@ &#& .@@@@ +@@@@@@@@# @@@8 @@@@@@@* .*@@@#. *@@ @@@& :#@@@o .@@: *&@8 @o o@@: @. @@@# *@@#. :8# .@@@@ +@@@@@@@@@ @@@@ &@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@# o@@@@ @@@@@ +@@@@@& &@@@@ 8@@@@@@@@@8&8@@@@@#8#@@@o8@#&@@o&@@@&@@8@@&@@@@88@@8#@8&@@##@@@@@@#8@@#8@@88@@@@@ *@@@@@@@ +@@@# #@@@@#. @@@@@@@@@@@@@8@@8#o@&#@@@@o.@o*@@*.@@@.@&:8o8*@@@8&@@#@@@8@@@@8@#@@@8&@@@@@@#@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +These values are expanded in banner.txt +========================================================== +expanded.project.version value is ${expanded.project.version} + +expanded.project.property value is ${expanded.project.property} +========================================================== \ No newline at end of file diff --git a/spring-boot-property-exp/property-exp-default/build.gradle b/spring-boot-property-exp/property-exp-default/build.gradle new file mode 100644 index 0000000000..f3c5f4a378 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/build.gradle @@ -0,0 +1,50 @@ +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE") + } +} + +apply plugin: 'java' +apply plugin: 'maven' +apply plugin: 'idea' +apply plugin: 'org.springframework.boot' + +group = 'com.baeldung' +version = '0.0.1-SNAPSHOT' + +description = """spring-boot""" + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} + +repositories { + mavenCentral() +} + +import org.apache.tools.ant.filters.ReplaceTokens +processResources { + with copySpec { + from 'src/main/resources' + include '**/application*.yml' + include '**/application*.yaml' + include '**/application*.properties' + project.properties.findAll().each { prop -> + + if (prop.value != null) { + filter(ReplaceTokens, tokens: [ (prop.key): prop.value]) + filter(ReplaceTokens, tokens: [ ('project.' + prop.key): prop.value]) + } + + } + } +} + +dependencies { + compile("org.springframework.boot:spring-boot-starter") +} diff --git a/spring-boot-property-exp/property-exp-default/gradle.properties b/spring-boot-property-exp/property-exp-default/gradle.properties new file mode 100644 index 0000000000..e0ec84d851 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/gradle.properties @@ -0,0 +1 @@ +custom.property=Custom Gradle Property \ No newline at end of file diff --git a/spring-boot-property-exp/property-exp-default/pom.xml b/spring-boot-property-exp/property-exp-default/pom.xml new file mode 100644 index 0000000000..3629e56111 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.4.RELEASE + + + com.baeldung + property-exp-default + 0.0.1-SNAPSHOT + jar + + property-exp-default + http://maven.apache.org + + + UTF-8 + Custom Property Value + + + + + org.springframework.boot + spring-boot-starter + + + + diff --git a/spring-boot-property-exp/property-exp-default/settings.gradle b/spring-boot-property-exp/property-exp-default/settings.gradle new file mode 100644 index 0000000000..faccf1c7f3 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'property-exp-default' diff --git a/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java b/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java new file mode 100644 index 0000000000..d88dbf0123 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/SpringBootPropertyExpansionApp.java @@ -0,0 +1,13 @@ +package com.baeldung.propertyexpansion; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootPropertyExpansionApp { + + public static void main(String[] args) { + SpringApplication.run(SpringBootPropertyExpansionApp.class, args); + } + +} diff --git a/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java b/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java new file mode 100644 index 0000000000..b7427d5eab --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/src/main/java/com/baeldung/propertyexpansion/components/PropertyLoggerBean.java @@ -0,0 +1,36 @@ +package com.baeldung.propertyexpansion.components; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class PropertyLoggerBean { + + private static final Logger log = LoggerFactory.getLogger(PropertyLoggerBean.class); + + @Value("${expanded.project.version}") + private String projectVersion; + + @Value("${expanded.project.property}") + private String projectProperty; + + @PostConstruct + public void printProperties() { + + log.info(""); + log.info("Properties logged from logger bean"); + log.info("==========================================="); + log.info("Project version : {}", projectVersion); + log.info("Project property : {}", projectProperty); + log.info("==========================================="); + log.info(""); + + } + + + +} diff --git a/spring-boot-property-exp/property-exp-default/src/main/resources/application.properties b/spring-boot-property-exp/property-exp-default/src/main/resources/application.properties new file mode 100644 index 0000000000..4e28ccadaa --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/src/main/resources/application.properties @@ -0,0 +1,2 @@ +expanded.project.version=@project.version@ +expanded.project.property=@custom.property@ \ No newline at end of file diff --git a/spring-boot-property-exp/property-exp-default/src/main/resources/banner.txt b/spring-boot-property-exp/property-exp-default/src/main/resources/banner.txt new file mode 100644 index 0000000000..20963b0447 --- /dev/null +++ b/spring-boot-property-exp/property-exp-default/src/main/resources/banner.txt @@ -0,0 +1,21 @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@#@@@@@########@@@@@@@@@@@@@@@@@@@@@@@@...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@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +These values are expanded in banner.txt +========================================================== +{expanded.project.version} is ${expanded.project.version} + +{expanded.project.property} is ${expanded.project.property} +========================================================== \ No newline at end of file