Skip to content

Commit 6c5524d

Browse files
authored
Merge pull request #14 from ericdallo/support-yaml-and-map
Support yaml and binding Map in POJO
2 parents a0804aa + ce37c9f commit 6c5524d

17 files changed

+615
-122
lines changed

README.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ _Maven_:
3030

3131
- Adding this annotation to any spring managed bean
3232
```java
33-
@S3PropertiesLocation("my-bucket/my-folder/my-properties.properties")
33+
@S3PropertiesLocation("my-bucket/my-folder/my-properties.yaml")
3434
```
3535
- Using a specific profile to only load properties if the app is running with that profile
3636
```java
@@ -39,10 +39,113 @@ _Maven_:
3939
- Load from a System env variable
4040
```java
4141
@S3PropertiesLocation(value = "${AWS_S3_LOCATION}", profiles = "developer")
42-
or
42+
// or
4343
@S3PropertiesLocation(value = "${AWS_S3_BUCKET}/application/my.properties", profiles = "developer")
4444
```
4545

46+
### Binding properties to a POJO
47+
You can bind the externally loaded properties to a POJO as well.
48+
49+
For e.g., if you have a YAML file as
50+
```yaml
51+
zuul:
52+
routes:
53+
query1:
54+
path: /api/apps/test1/query/**
55+
stripPrefix: false
56+
url: "https://test.url.com/query1"
57+
query2:
58+
path: /api/apps/test2/query/**
59+
stripPrefix: false
60+
url: "https://test.url.com/query2"
61+
index1:
62+
path: /api/apps/*/index/**
63+
stripPrefix: false
64+
url: "https://test.url.com/index"
65+
```
66+
Then you can bind the properties to a POJO using ConfigurationProperties:
67+
```java
68+
@Component
69+
@ConfigurationProperties("zuul")
70+
public class RouteConfig {
71+
private Map<String, Map<String, String>> routes = new HashMap<>();
72+
73+
public void setRoutes(Map<String, Map<String, String>> routes) {
74+
this.routes = routes;
75+
}
76+
77+
public Map<String, Map<String, String>> getRoutes() {
78+
return routes;
79+
}
80+
}
81+
82+
// or
83+
84+
@Component
85+
@ConfigurationProperties("zuul")
86+
public class RouteConfig {
87+
private Map<String, Route> routes;
88+
89+
public void setRoutes(Map<String, Route> routes) {
90+
this.routes = routes;
91+
}
92+
93+
public Map<String, Route> getRoutes() {
94+
return routes;
95+
}
96+
97+
public static class Route {
98+
private String path;
99+
private boolean stripPrefix;
100+
String url;
101+
102+
public String getPath() {
103+
return path;
104+
}
105+
106+
public void setPath(String path) {
107+
this.path = path;
108+
}
109+
110+
public boolean isStripPrefix() {
111+
return stripPrefix;
112+
}
113+
114+
public void setStripPrefix(boolean stripPrefix) {
115+
this.stripPrefix = stripPrefix;
116+
}
117+
118+
public String getUrl() {
119+
return url;
120+
}
121+
122+
public void setUrl(String url) {
123+
this.url = url;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
try {
129+
return new ObjectMapper().writeValueAsString(this);
130+
} catch (JsonProcessingException e) {
131+
e.printStackTrace();
132+
}
133+
return this.toString();
134+
}
135+
}
136+
137+
@Override
138+
public String toString() {
139+
try {
140+
return new ObjectMapper().writeValueAsString(this);
141+
} catch (JsonProcessingException e) {
142+
e.printStackTrace();
143+
}
144+
return this.toString();
145+
}
146+
}
147+
```
148+
46149
### Refreshing properties in runtime
47150

48151
You can force your application to load properties from S3 again without restart. _S3 Properties Loader_ uses a [Spring Cloud](http://projects.spring.io/spring-cloud/) feature that allows the spring beans annotated with `@RefreshScope` to reload properties.

build.gradle

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
apply plugin: 'java'
2-
apply plugin: 'eclipse'
3-
apply plugin: 'application'
4-
apply plugin: 'net.researchgate.release'
5-
apply plugin: 'maven-publish'
6-
apply plugin: 'maven'
7-
apply plugin: 'signing'
8-
apply plugin: 'com.jfrog.bintray'
1+
plugins {
2+
id 'java'
3+
id 'eclipse'
4+
id 'application'
5+
id 'net.researchgate.release' version '2.6.0'
6+
id 'maven-publish'
7+
id 'maven'
8+
id 'signing'
9+
id 'com.jfrog.bintray' version '1.8.5'
10+
}
911

1012
compileJava.options.encoding = 'UTF-8'
1113

1214
group = 'com.spring.loader'
1315
archivesBaseName = 's3-loader'
1416

15-
buildscript {
16-
repositories {
17-
mavenLocal()
18-
mavenCentral()
19-
jcenter()
20-
}
21-
22-
dependencies {
23-
classpath 'net.researchgate:gradle-release:2.3.5'
24-
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
25-
}
26-
}
27-
2817
eclipse {
2918
classpath {
3019
downloadJavadoc = true
@@ -33,12 +22,12 @@ eclipse {
3322
}
3423

3524
release {
36-
failOnCommitNeeded = false
37-
failOnPublishNeeded = true
38-
failOnSnapshotDependencies = true
39-
failOnUnversionedFiles = true
40-
failOnUpdateNeeded = true
41-
revertOnFail = true
25+
failOnCommitNeeded = false
26+
failOnPublishNeeded = true
27+
failOnSnapshotDependencies = true
28+
failOnUnversionedFiles = true
29+
failOnUpdateNeeded = true
30+
revertOnFail = true
4231
}
4332

4433
afterReleaseBuild.dependsOn publish
@@ -61,25 +50,32 @@ repositories {
6150
}
6251

6352
dependencies {
64-
compile "org.springframework:spring-context:4.3.4.RELEASE"
65-
compile "org.springframework.cloud:spring-cloud-aws-core:1.1.3.RELEASE"
66-
compile "org.springframework.cloud:spring-cloud-context:1.1.8.RELEASE"
67-
68-
testCompile "junit:junit:4.12"
69-
testCompile "org.mockito:mockito-core:1.10.19"
70-
testCompile "org.assertj:assertj-core:1.0.0"
71-
testCompile "org.powermock:powermock-api-mockito:1.6.6"
72-
testCompile "org.powermock:powermock-module-junit4:1.6.6"
53+
implementation "org.springframework:spring-context:5.3.0"
54+
implementation "org.springframework.cloud:spring-cloud-aws-core:2.2.4.RELEASE"
55+
implementation "org.springframework.cloud:spring-cloud-context:2.2.5.RELEASE"
56+
57+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
58+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
59+
60+
testImplementation "org.assertj:assertj-core:3.18.0"
61+
testImplementation "org.mockito:mockito-inline:3.6.0"
62+
testImplementation "org.mockito:mockito-junit-jupiter:3.6.0"
63+
testRuntimeOnly "org.yaml:snakeyaml:1.27"
7364
}
7465

7566
task sourcesJar(type: Jar) {
7667
from sourceSets.main.allSource
77-
classifier = 'sources'
68+
archiveClassifier = 'sources'
7869
}
7970

8071
artifacts {
8172
archives jar
8273
archives sourcesJar
8374
}
8475

76+
// Make all tests use JUnit 5
77+
tasks.withType(Test) {
78+
useJUnitPlatform()
79+
}
80+
8581
mainClassName = ''

gradle/wrapper/gradle-wrapper.jar

57.8 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)