Thursday, 2 August 2018

Spring boot: @ConfigurationProperties: map properties by prefix

By using @ConfigurationProperties annotation, you can map the properties by prefix.

For example,

beta.appName="ChatServer_beta"
beta.appVersion="1.5Beta"
beta.servers[0].serverIp="5.6.7.8"
beta.servers[0].serverEnvironemnt="Testing"
beta.servers[1].serverIp="1.2.3.4"
beta.servers[1].serverEnvironemnt="Development"

You can map above properties like below.

@Configuration
@ConfigurationProperties(prefix="beta")
public class Service {
         private String appName;
         private String appVersion;
         private List<Server> servers;
        
         ....
         ....
         ....
}

Find the below working application.

application.properties

beta.appName="ChatServer_beta"
beta.appVersion="1.5Beta"
beta.servers[0].serverIp="5.6.7.8"
beta.servers[0].serverEnvironemnt="Testing"
beta.servers[1].serverIp="1.2.3.4"
beta.servers[1].serverEnvironemnt="Development"

alpha.appName="ChatServer_alpha"
alpha.appVersion="1.5alpha"
alpha.servers[0].serverIp="5.6.7.8"
alpha.servers[0].serverEnvironemnt="Testing"
alpha.servers[1].serverIp="1.2.3.4"
alpha.servers[1].serverEnvironemnt="Development"

Server.java

package com.sample.myApp.model;

public class Server {
 private String serverIp;
 private String serverEnvironemnt;

 public String getServerIp() {
  return serverIp;
 }

 public void setServerIp(String serverIp) {
  this.serverIp = serverIp;
 }

 public String getServerEnvironemnt() {
  return serverEnvironemnt;
 }

 public void setServerEnvironemnt(String serverEnvironemnt) {
  this.serverEnvironemnt = serverEnvironemnt;
 }

}

Service.java

package com.sample.myApp.model;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix="alpha")
public class Service {
 private String appName;
 private String appVersion;
 private List<Server> servers;

 public String getAppName() {
  return appName;
 }

 public void setAppName(String appName) {
  this.appName = appName;
 }

 public String getAppVersion() {
  return appVersion;
 }

 public void setAppVersion(String appVersion) {
  this.appVersion = appVersion;
 }

 public List<Server> getServers() {
  return servers;
 }

 public void setServers(List<Server> servers) {
  this.servers = servers;
 }

}

Application.java

package com.sample.myApp;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Service service = applicationContext.getBean(Service.class);
  String appName = service.getAppName();
  String appVersion = service.getAppVersion();

  System.out.println("*******************************************");
  System.out.printf("Application Name : %s\n", appName);
  System.out.printf("Application Version %s\n", appVersion);

  System.out.println("Servers Information");
  
  List<Server> servers = service.getServers();

  for (Server server : servers) {
   System.out.printf("Environment : %s\n", server.getServerEnvironemnt());
   System.out.printf("Server Ip : %s\n", server.getServerIp());
  }

  System.out.println("*******************************************");
  applicationContext.close();

 }

}


When you ran ‘Application.java’, you can able to see below messages in the console.

*******************************************
Application Name : "ChatServer_alpha"
Application Version "1.5alpha"
Servers Information
Environment : "Testing"
Server Ip : "5.6.7.8"
Environment : "Development"
Server Ip : "1.2.3.4"
*******************************************



Previous                                                 Next                                                 Home

Spring Boot: @ConfigurationProperties tutorial

In my previous post, I explained how to inject a property from 'application.properties' file using @Value annotation. Spring boot provides more elegant way, where you can map the properties to a model class using '@ConfigurationProperties' anotation.

application.properties
appName="ChatServer_beta"
appVersion="1.5Beta"
servers[0].serverIp="5.6.7.8"
servers[0].serverEnvironemnt="Testing"
servers[1].serverIp="1.2.3.4"
servers[1].serverEnvironemnt="Development"

We can map above properties to below model class.

@Configuration
@ConfigurationProperties
public class Service {
         private String appName;
         private String appVersion;
         private List<Server> servers;
        
         ....
         ....
         ....
}

Find the below working application.

Server.java
package com.sample.myApp.model;

public class Server {
 private String serverIp;
 private String serverEnvironemnt;

 public String getServerIp() {
  return serverIp;
 }

 public void setServerIp(String serverIp) {
  this.serverIp = serverIp;
 }

 public String getServerEnvironemnt() {
  return serverEnvironemnt;
 }

 public void setServerEnvironemnt(String serverEnvironemnt) {
  this.serverEnvironemnt = serverEnvironemnt;
 }

}


Service.java
package com.sample.myApp.model;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties
public class Service {
 private String appName;
 private String appVersion;
 private List<Server> servers;

 public String getAppName() {
  return appName;
 }

 public void setAppName(String appName) {
  this.appName = appName;
 }

 public String getAppVersion() {
  return appVersion;
 }

 public void setAppVersion(String appVersion) {
  this.appVersion = appVersion;
 }

 public List<Server> getServers() {
  return servers;
 }

 public void setServers(List<Server> servers) {
  this.servers = servers;
 }

}

Application.java

package com.sample.myApp;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Service service = applicationContext.getBean(Service.class);
  String appName = service.getAppName();
  String appVersion = service.getAppVersion();

  System.out.println("*******************************************");
  System.out.printf("Application Name : %s\n", appName);
  System.out.printf("Application Version %s\n", appVersion);

  System.out.println("Servers Information");
  
  List<Server> servers = service.getServers();

  for (Server server : servers) {
   System.out.printf("Environment : %s\n", server.getServerEnvironemnt());
   System.out.printf("Server Ip : %s\n", server.getServerIp());
  }

  System.out.println("*******************************************");
  applicationContext.close();

 }

}

When you ran 'Application.java', you can able to see below messages in the console.

*******************************************
Application Name : "ChatServer_beta"
Application Version "1.5Beta"
Servers Information
Environment : "Testing"
Server Ip : "5.6.7.8"
Environment : "Development"
Server Ip : "1.2.3.4"
*******************************************






Previous                                                 Next                                                 Home

Wednesday, 1 August 2018

Spring boot: configure yaml properties using prefix

You can bind the properties using the prefix. For example, below file has the properties that are specific to beta and alpha version of the software.


application.yml
---
beta:
  appName: "ChatServer_beta"
  appVersion: "1.5Beta"
  servers:
    - serverIp: "5.6.7.8"
      serverEnvironemnt: "Testing"
    - serverIp: "1.2.3.4"
      serverEnvironemnt: "Development"
---
alpha:
  appName: "ChatServer_Alpha"
  appVersion: "1.5Alpha"
  servers:
    - serverIp: "15.16.17.18"
      serverEnvironemnt: "Testing"
    - serverIp: "11.12.13.14"
      serverEnvironemnt: "Development"


You can bind beta properties by using below statements.

@Configuration
@ConfigurationProperties(prefix="beta")
public class Service {
         private String appName;
         private String appVersion;
         private List<Server> servers;
        
         ....
         ....
         ....
}

You can bind alpha properties by using below statements.

@Configuration
@ConfigurationProperties(prefix="alpha")
public class Service {
         private String appName;
         private String appVersion;
         private List<Server> servers;
        
         ....
         ....
         ....
}

Find the below working application.

Server.java
package com.sample.myApp.model;

public class Server {
 private String serverIp;
 private String serverEnvironemnt;

 public String getServerIp() {
  return serverIp;
 }

 public void setServerIp(String serverIp) {
  this.serverIp = serverIp;
 }

 public String getServerEnvironemnt() {
  return serverEnvironemnt;
 }

 public void setServerEnvironemnt(String serverEnvironemnt) {
  this.serverEnvironemnt = serverEnvironemnt;
 }

}

Service.java
package com.sample.myApp.model;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix="beta")
public class Service {
 private String appName;
 private String appVersion;
 private List<Server> servers;

 public String getAppName() {
  return appName;
 }

 public void setAppName(String appName) {
  this.appName = appName;
 }

 public String getAppVersion() {
  return appVersion;
 }

 public void setAppVersion(String appVersion) {
  this.appVersion = appVersion;
 }

 public List<Server> getServers() {
  return servers;
 }

 public void setServers(List<Server> servers) {
  this.servers = servers;
 }

}

Application.java
package com.sample.myApp;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Service service = applicationContext.getBean(Service.class);
  String appName = service.getAppName();
  String appVersion = service.getAppVersion();

  System.out.println("*******************************************");
  System.out.printf("Application Name : %s\n", appName);
  System.out.printf("Application Version %s\n", appVersion);

  System.out.println("Servers Information");
  
  List<Server> servers = service.getServers();

  for (Server server : servers) {
   System.out.printf("Environment : %s\n", server.getServerEnvironemnt());
   System.out.printf("Server Ip : %s\n", server.getServerIp());
  }

  System.out.println("*******************************************");
  applicationContext.close();

 }

}

When you ran above application, you can able to see below messages in the console.
*******************************************
Application Name : ChatServer_beta
Application Version 1.5Beta
Servers Information
Environment : Testing
Server Ip : 5.6.7.8
Environment : Development
Server Ip : 1.2.3.4
*******************************************


Project structure looks like below.









Previous                                                 Next                                                 Home

Spring yaml configuration

YAML is a human readable data serialization language. Spring supports yaml based configuration. If you are new to YAML, I would recommend you to go through my below tutorial.
https://self-learning-java-tutorial.blogspot.com/2018/06/yaml-tutorial.html



application.yml
---
appName: "ChatServer"
appVersion: "1.5"
servers:
- serverIp: "5.6.7.8"
  serverEnvironemnt: "Testing"
- serverIp: "1.2.3.4"
  serverEnvironemnt: "Development"

You can map above yaml data to java model classes using @ConfigurationProperties annotation.

@Configuration
@ConfigurationProperties
public class Service {
         private String appName;
         private String appVersion;
         private List<Server> servers;
        
         ....
         ....
}

Find the below working application.

Server.java
package com.sample.myApp.model;

public class Server {
 private String serverIp;
 private String serverEnvironemnt;

 public String getServerIp() {
  return serverIp;
 }

 public void setServerIp(String serverIp) {
  this.serverIp = serverIp;
 }

 public String getServerEnvironemnt() {
  return serverEnvironemnt;
 }

 public void setServerEnvironemnt(String serverEnvironemnt) {
  this.serverEnvironemnt = serverEnvironemnt;
 }

}

Service.java
package com.sample.myApp.model;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties
public class Service {
 private String appName;
 private String appVersion;
 private List<Server> servers;

 public String getAppName() {
  return appName;
 }

 public void setAppName(String appName) {
  this.appName = appName;
 }

 public String getAppVersion() {
  return appVersion;
 }

 public void setAppVersion(String appVersion) {
  this.appVersion = appVersion;
 }

 public List<Server> getServers() {
  return servers;
 }

 public void setServers(List<Server> servers) {
  this.servers = servers;
 }

}

Application.java
package com.sample.myApp;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Service service = applicationContext.getBean(Service.class);
  String appName = service.getAppName();
  String appVersion = service.getAppVersion();

  System.out.println("*******************************************");
  System.out.printf("Application Name : %s\n", appName);
  System.out.printf("Application Version %s\n", appVersion);

  System.out.println("Servers Information");
  
  List<Server> servers = service.getServers();

  for (Server server : servers) {
   System.out.printf("Environment : %s\n", server.getServerEnvironemnt());
   System.out.printf("Server Ip : %s\n", server.getServerIp());
  }

  System.out.println("*******************************************");
  applicationContext.close();

 }

}


When you ran ‘Application.java’, you can able to see below messages in the console.

*******************************************
Application Name : ChatServer
Application Version 1.5
Servers Information
Environment : Testing
Server Ip : 5.6.7.8
Environment : Development
Server Ip : 1.2.3.4
*******************************************


Project structure looks like below.






Previous                                                 Next                                                 Home

Spring boot: place holders in properties file

You can refer the properties by using their name. See the below example.

application.properties
app.name=MyApp
app.version=1.5
app.description=${app.name} : ${app.version} is running using spring boot application

As you see, I am referring ‘app.name’ and ‘app.version’ in ‘app.description’ property.

ConfigBean.java
package com.sample.myApp.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConfigBean {

 @Value("${app.name}")
 private String appName;

 @Value("${app.version}")
 private String appVersion;

 @Value("${app.description}")
 private String appDescription;

 public String getAppName() {
  return appName;
 }

 public void setAppName(String appName) {
  this.appName = appName;
 }

 public String getAppVersion() {
  return appVersion;
 }

 public void setAppVersion(String appVersion) {
  this.appVersion = appVersion;
 }

 public String getAppDescription() {
  return appDescription;
 }

 public void setAppDescription(String appDescription) {
  this.appDescription = appDescription;
 }

}

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.ConfigBean;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  ConfigBean configBean = applicationContext.getBean(ConfigBean.class);

  String applicationName = configBean.getAppName();
  String applicationVersion = configBean.getAppVersion();
  String applicationDescription = configBean.getAppDescription();

  System.out.println("**********************************************");
  System.out.printf("Application Name : %s\n", applicationName);
  System.out.printf("Application Version : %s\n", applicationVersion);
  System.out.printf("Application Description : %s\n", applicationDescription);
  System.out.println("**********************************************");
  
  applicationContext.close();

 }

}

When you ran ‘Application.java’, you can able to see below messages in the console.

**********************************************
Application Name : MyApp
Application Version : 1.5
Application Description : MyApp : 1.5 is running using spring boot application
**********************************************


Project structure looks like below.





Previous                                                 Next                                                 Home