Skip to content

Commit 3184c49

Browse files
roman-seleznovadamd1985
authored andcommitted
BAEL-764 Automatic Property Expansion with Spring Boot (eugenp#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 1bdc544. * BAEL-764 Automatic Property Expansion with Spring Boot * BAEL-764 Automatic Property Expansion with Spring Boot * BAEL-764 Automatic Property Expansion with Spring Boot
1 parent faeaf0d commit 3184c49

16 files changed

Lines changed: 325 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
<module>spring-drools</module>
227227
<module>drools</module>
228228
<module>liquibase</module>
229+
<module>spring-boot-property-exp</module>
229230
</modules>
230231

231232
<dependencies>

‎spring-boot-property-exp/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## The Module Holds Sources for the Following Articles
2+
- [Automatic Property Expansion with Spring Boot] (http://www.baeldung.com/property-expansion-spring-boot)

‎spring-boot-property-exp/pom.xml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>spring-boot-property-exp</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
17+
<packaging>pom</packaging>
18+
19+
<name>spring-boot-property-exp</name>
20+
<url>http://maven.apache.org</url>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
</properties>
25+
26+
<modules>
27+
<module>property-exp-default</module>
28+
<module>property-exp-custom</module>
29+
</modules>
30+
31+
32+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<parent>
5+
<artifactId>spring-boot-property-exp</artifactId>
6+
<groupId>com.baeldung</groupId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>property-exp-custom</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
16+
<name>property-exp-custom</name>
17+
<url>http://maven.apache.org</url>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<custom.property>Custom Property Value</custom.property>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
<version>1.5.4.RELEASE</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<resources>
34+
<resource>
35+
<directory>${basedir}/src/main/resources</directory>
36+
<filtering>true</filtering>
37+
<includes>
38+
<include>**/application*.yml</include>
39+
<include>**/application*.yaml</include>
40+
<include>**/application*.properties</include>
41+
</includes>
42+
</resource>
43+
</resources>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-resources-plugin</artifactId>
48+
<version>2.7</version>
49+
<configuration>
50+
<delimiters>
51+
<delimiter>@</delimiter>
52+
</delimiters>
53+
<useDefaultDelimiters>true</useDefaultDelimiters>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.propertyexpansion;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootPropertyExpansionApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootPropertyExpansionApp.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.propertyexpansion.components;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.PostConstruct;
9+
10+
@Component
11+
public class PropertyLoggerBean {
12+
13+
private static final Logger log = LoggerFactory.getLogger(PropertyLoggerBean.class);
14+
15+
@Value("${expanded.project.version}")
16+
private String projectVersion;
17+
18+
@Value("${expanded.project.property}")
19+
private String projectProperty;
20+
21+
22+
@PostConstruct
23+
public void printProperties() {
24+
25+
log.info("");
26+
log.info("Properties logged in a bean:");
27+
log.info("===========================================");
28+
log.info("Project version : {}", projectVersion);
29+
log.info("Project property : {}", projectProperty);
30+
log.info("===========================================");
31+
log.info("");
32+
33+
}
34+
35+
36+
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
expanded.project.version=${project.version}
2+
expanded.project.property=${custom.property}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2+
@@@@@@@@@@@@@@@@@@@@@@@@@#@@@@@########@@@@@@@@@@@@@@@@@@@@@@@@...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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
14+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
15+
16+
These values are expanded in banner.txt
17+
==========================================================
18+
expanded.project.version value is ${expanded.project.version}
19+
20+
expanded.project.property value is ${expanded.project.property}
21+
==========================================================
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
7+
}
8+
}
9+
10+
apply plugin: 'java'
11+
apply plugin: 'maven'
12+
apply plugin: 'idea'
13+
apply plugin: 'org.springframework.boot'
14+
15+
group = 'com.baeldung'
16+
version = '0.0.1-SNAPSHOT'
17+
18+
description = """spring-boot"""
19+
20+
sourceCompatibility = 1.8
21+
targetCompatibility = 1.8
22+
tasks.withType(JavaCompile) {
23+
options.encoding = 'UTF-8'
24+
}
25+
26+
repositories {
27+
mavenCentral()
28+
}
29+
30+
import org.apache.tools.ant.filters.ReplaceTokens
31+
processResources {
32+
with copySpec {
33+
from 'src/main/resources'
34+
include '**/application*.yml'
35+
include '**/application*.yaml'
36+
include '**/application*.properties'
37+
project.properties.findAll().each { prop ->
38+
39+
if (prop.value != null) {
40+
filter(ReplaceTokens, tokens: [ (prop.key): prop.value])
41+
filter(ReplaceTokens, tokens: [ ('project.' + prop.key): prop.value])
42+
}
43+
44+
}
45+
}
46+
}
47+
48+
dependencies {
49+
compile("org.springframework.boot:spring-boot-starter")
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom.property=Custom Gradle Property

0 commit comments

Comments
 (0)