Why Spring Configuration?
Spring Cloud Config provides support for externalized configuration in a distributed system. It allows you to dynamically push updates to configuration properties to the application without needing to restart or redeploy.
Step-by-Step Guide to Integrate With a PCF Application
Step 1: Add a Maven Dependency
XML
xxxxxxxxxx
1
23
1
<dependency>
2
3
<groupId>io.pivotal.spring.cloud</groupId>
4
5
<artifactId>spring-cloud-services-starter-config-client</artifactId>
6
7
</dependency>
8
9
<dependency>
10
11
<groupId>org.springframework.cloud</groupId>
12
13
<artifactId>spring-cloud-starter-config</artifactId>
14
15
</dependency>
16
17
<dependency>
18
19
<groupId>org.springframework.boot</groupId>
20
21
<artifactId>spring-boot-starter-actuator</artifactId>
22
23
</dependency>
pom.xml
XML
xxxxxxxxxx
1
113
1
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
<modelVersion>4.0.0</modelVersion>
6
7
<groupId>gsahoo.demo</groupId>
8
<artifactId>aSpringConfigDemo</artifactId>
9
<version>1.0.0</version>
10
<packaging>jar</packaging>
11
12
<name>aspringconfig-demo</name>
13
<description>Spring Boot with spring config</description>
14
<parent>
15
<groupId>org.springframework.boot</groupId>
16
<artifactId>spring-boot-starter-parent</artifactId>
17
<version>2.3.0.RELEASE</version>
18
<relativePath ></relativePath> <!-- lookup parent from repository -->
19
</parent>
20
21
<properties>
22
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24
<java.version>1.8</java.version>
25
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
26
</properties>
27
<dependencies>
28
<dependency>
29
<groupId>junit</groupId>
30
<artifactId>junit</artifactId>
31
<version>3.8.1</version>
32
<scope>test</scope>
33
</dependency>
34
35
<dependency>
36
<groupId>org.springframework.boot</groupId>
37
<artifactId>spring-boot-starter</artifactId>
38
</dependency>
39
40
41
42
<dependency>
43
<groupId>com.fasterxml.jackson.core</groupId>
44
<artifactId>jackson-databind</artifactId>
45
</dependency>
46
<dependency>
47
<groupId>org.projectlombok</groupId>
48
<artifactId>lombok</artifactId>
49
</dependency>
50
51
52
<dependency>
53
<groupId>io.springfox</groupId>
54
<artifactId>springfox-swagger2</artifactId>
55
<version>2.6.1</version>
56
</dependency>
57
58
<dependency>
59
<groupId>io.springfox</groupId>
60
<artifactId>springfox-swagger-ui</artifactId>
61
<version>2.6.1</version>
62
</dependency>
63
<dependency>
64
<groupId>org.springframework.boot</groupId>
65
<artifactId>spring-boot-starter-web</artifactId>
66
</dependency>
67
<dependency>
68
<groupId>io.pivotal.spring.cloud</groupId>
69
<artifactId>spring-cloud-services-starter-config-client</artifactId>
70
</dependency>
71
72
<dependency>
73
<groupId>org.springframework.cloud</groupId>
74
<artifactId>spring-cloud-starter-config</artifactId>
75
</dependency>
76
<dependency>
77
<groupId>org.springframework.boot</groupId>
78
<artifactId>spring-boot-starter-actuator</artifactId>
79
</dependency>
80
81
</dependencies>
82
<dependencyManagement>
83
<dependencies>
84
85
<dependency>
86
<groupId>org.springframework.cloud</groupId>
87
<artifactId>spring-cloud-dependencies</artifactId>
88
<version>${spring-cloud.version}</version>
89
<type>pom</type>
90
<scope>import</scope>
91
</dependency>
92
<dependency>
93
<groupId>io.pivotal.spring.cloud</groupId>
94
<artifactId>spring-cloud-services-dependencies</artifactId>
95
<version>2.2.1.RELEASE</version>
96
<type>pom</type>
97
<scope>import</scope>
98
</dependency>
99
100
</dependencies>
101
</dependencyManagement>
102
<build>
103
<plugins>
104
<plugin>
105
<groupId>org.springframework.boot</groupId>
106
<artifactId>spring-boot-maven-plugin</artifactId>
107
</plugin>
108
</plugins>
109
</build>
110
</project>
Step 2: Add refreshscope and EnableAutoConfiguration to the Model Class
Java
x
1
package gsahoo.demo.model;
2
3
import org.springframework.beans.factory.annotation.Value;
4
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5
import org.springframework.cloud.context.config.annotation.RefreshScope;
6
import org.springframework.context.annotation.Configuration;
7
import org.springframework.stereotype.Component;
8
9
10
11
12
13
14
15
16
17
public class Person {
18
19
("${person.company}")
20
21
String company = null;
22
23
public void setCompany(String company) {
24
25
this.company = company;
26
27
}
28
29
public String getCompany() {
30
31
return company;
32
33
}
34
35
}
If you are autowiring this model object in any other class then make sure to add refreshscope to that class as well.