Spring Config Integration With a PCF Application: A Step-by-Step Guide

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
 






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
@Configuration
10

          
11
@EnableAutoConfiguration
12

          
13
@RefreshScope
14

          
15
@Component
16

          
17
public class Person {
18

          
19
    @Value("${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.