BAEL-764 Automatic Property Expansion with Spring Boot (#2133)

* Create pom.xml

Initial import

* First submit

* Second submit

* Different Types of Bean Injection in Spring

* Different Types of Bean Injection in Spring

* Added spring-core-di into the main build

* Revert "Create pom.xml"

This reverts commit 1bdc5443125df19575605f41ab28c9e8b6c69a32.

* BAEL-764 Automatic Property Expansion with Spring Boot

* BAEL-764 Automatic Property Expansion with Spring Boot

* BAEL-764 Automatic Property Expansion with Spring Boot
This commit is contained in:
Roman Seleznov 2017-06-30 18:20:46 +01:00 committed by adamd1985
parent faeaf0de6a
commit 3184c49da5
16 changed files with 325 additions and 0 deletions

View File

@ -226,6 +226,7 @@
<module>spring-drools</module>
<module>drools</module>
<module>liquibase</module>
<module>spring-boot-property-exp</module>
</modules>
<dependencies>

View File

@ -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)

View File

@ -0,0 +1,32 @@
<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">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-boot-property-exp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>spring-boot-property-exp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>property-exp-default</module>
<module>property-exp-custom</module>
</modules>
</project>

View File

@ -0,0 +1,60 @@
<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">
<parent>
<artifactId>spring-boot-property-exp</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>property-exp-custom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>property-exp-custom</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<custom.property>Custom Property Value</custom.property>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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("");
}
}

View File

@ -0,0 +1,2 @@
expanded.project.version=${project.version}
expanded.project.property=${custom.property}

View File

@ -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}
==========================================================

View File

@ -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")
}

View File

@ -0,0 +1 @@
custom.property=Custom Gradle Property

View File

@ -0,0 +1,33 @@
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<groupId>com.baeldung</groupId>
<artifactId>property-exp-default</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>property-exp-default</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<custom.property>Custom Property Value</custom.property>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1 @@
rootProject.name = 'property-exp-default'

View File

@ -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);
}
}

View File

@ -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("");
}
}

View File

@ -0,0 +1,2 @@
expanded.project.version=@project.version@
expanded.project.property=@custom.property@

View File

@ -0,0 +1,21 @@
@@@@@@@@@@@@@@@@@@@@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8o8*@@@8&@@#@@@8@@@@8@#@@@8&@@@@@@#@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
These values are expanded in banner.txt
==========================================================
{expanded.project.version} is ${expanded.project.version}
{expanded.project.property} is ${expanded.project.property}
==========================================================