Saturday, 4 August 2018

Spring boot: Properties Conversion

When you use, @ConfigurationProperties annotation, spring automatically binds the properties to the target type.

For example,

application.properties
appName=Chat Server
version=1.25
noOfDevelopers=10
isReadyForRelease=true

As you see above file
a.   appName is of type String,
b.   version is of type double
c.   noOfDevelopers is of type integer
d.   isReadyForRelease is of type boolean

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

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class MyConfigurations {

 private String appName;
 private double version;
 private int noOfDevelopers;
 private boolean isReadyForRelease;

 public String getAppName() {
  return appName;
 }

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

 public double getVersion() {
  return version;
 }

 public void setVersion(double version) {
  this.version = version;
 }

 public int getNoOfDevelopers() {
  return noOfDevelopers;
 }

 public void setNoOfDevelopers(int noOfDevelopers) {
  this.noOfDevelopers = noOfDevelopers;
 }

 public boolean isReadyForRelease() {
  return isReadyForRelease;
 }

 public void setReadyForRelease(boolean isReadyForRelease) {
  this.isReadyForRelease = isReadyForRelease;
 }

}

If you use @ConfigurationProperties, then spring automatically takes care of type mapping.

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.MyConfigurations;

@SpringBootApplication
public class Application {

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

  MyConfigurations myConfiguration = applicationContext.getBean(MyConfigurations.class);

  System.out.println("****************************************************");
  System.out.printf("Application Name : %s\n", myConfiguration.getAppName());
  System.out.printf("Number Of Developers : %d\n", myConfiguration.getNoOfDevelopers());
  System.out.printf("Version : %f\n", myConfiguration.getVersion());
  System.out.printf("Is Ready For Release : %b\n", myConfiguration.isReadyForRelease());
  System.out.println("****************************************************");

  applicationContext.close();

 }

}

When you ran ‘Application.java’, you can able to see below messages in the console.
****************************************************
Application Name : Chat Server
Number Of Developers : 10
Version : 1.250000
Is Ready For Release : false
****************************************************


How to perform custom type conversion?
By using any of the below ways, you can perform custom type conversion.
a.   You can provide a ConversionService bean with a bean named conversionService
b.   By using custom property editors through a CustomEditorConfigurer bean
c.   By using Custom Converters with bean definitions annotated as @ConfigurationPropertiesBinding.

I am going to show, how to perform custom type conversion using approach ‘c’.

By using Custom Converters with bean definitions annotated as @ConfigurationPropertiesBinding
application.properties
appName=Chat Server
version=1.25
releaseDate=05-21-2018
supportEndDate=05-21-2028

I written ‘DateConverter’ class, that converts a string to date.

DateConverter.java
package com.sample.myApp.converters;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
@ConfigurationPropertiesBinding
public class DateConverter implements Converter<String, LocalDate> {
 @Override
 public LocalDate convert(String source) {
  if (source == null) {
   return null;
  }
  return LocalDate.parse(source, DateTimeFormatter.ofPattern("MM-dd-yyyy"));
 }
}

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

import java.time.LocalDate;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class MyConfigurations {

 private String appName;
 private double version;
 private LocalDate releaseDate;
 private LocalDate supportEndDate;

 public String getAppName() {
  return appName;
 }

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

 public double getVersion() {
  return version;
 }

 public void setVersion(double version) {
  this.version = version;
 }

 public LocalDate getReleaseDate() {
  return releaseDate;
 }

 public void setReleaseDate(LocalDate releaseDate) {
  this.releaseDate = releaseDate;
 }

 public LocalDate getSupportEndDate() {
  return supportEndDate;
 }

 public void setSupportEndDate(LocalDate supportEndDate) {
  this.supportEndDate = supportEndDate;
 }

}

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.MyConfigurations;

@SpringBootApplication
public class Application {

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

  MyConfigurations myConfiguration = applicationContext.getBean(MyConfigurations.class);

  System.out.println("****************************************************");
  System.out.printf("Application Name : %s\n", myConfiguration.getAppName());
  System.out.printf("Version : %f\n", myConfiguration.getVersion());
  System.out.printf("Release Date %s\n", myConfiguration.getReleaseDate().toString());
  System.out.printf("Support End Date %s\n", myConfiguration.getSupportEndDate().toString());
  System.out.println("****************************************************");

  applicationContext.close();

 }

}

When you ran ‘Application.java’, you can able to see below messages in the console.
****************************************************
Application Name : Chat Server
Version : 1.250000
Release Date 2018-05-21
Support End Date 2028-05-21
****************************************************

Project structure looks like below.



Previous                                                 Next                                                 Home

Spring boot: Relaxed Binding

Spring boot supports relaxed binding while mapping environment properties using @ConfigurationProperties beans, so there is no need to be exact match between property names and bean properties.

For example,
a. Kebab case variables can be mapped to camel case.
app-name can be mapped to appName

b. case insensitive mapping
PORT can be mapped to port

Find the below working application.

application.properties
beta.app-name="ChatServer_beta"
beta.app-version="1.5Beta"
beta.servers[0].server-ip="5.6.7.8"
beta.servers[0].server-environemnt="Testing"
beta.servers[1].server-ip="1.2.3.4"
beta.servers[1].server-environemnt="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;

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;
 }

}

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

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

@Configuration
public class MyConfiguration {

 @ConfigurationProperties(prefix = "beta")
 @Bean
 public Service betaService() {
  return new Service();
 }

}

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.MyConfiguration;
import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {

 private static void printServiceInfo(Service service) {
  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("*******************************************\n\n");
 }

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

  MyConfiguration myConfiguration = applicationContext.getBean(MyConfiguration.class);
  printServiceInfo(myConfiguration.betaService());

  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"
*******************************************


Project structure looks like below.





Previous                                                 Next                                                 Home

Spring boot: Configure bean from application environment properties

You can configure a bean from application properties(application.properties) file.

Example
@ConfigurationProperties(prefix = "alpha")
@Bean
public Service alphaService() {
         return new Service();
}

Above statements populate the Service bean based on the properties prefixed with ‘alpha’.

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="15.16.17.18"
alpha.servers[0].serverEnvironemnt="Testing"
alpha.servers[1].serverIp="11.21.31.41"
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;

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;
 }

}


MyConfiguration.java

package com.sample.myApp.model;

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

@Configuration
public class MyConfiguration {

 @ConfigurationProperties(prefix = "alpha")
 @Bean
 public Service alphaService() {
  return new Service();
 }
 
 @ConfigurationProperties(prefix = "beta")
 @Bean
 public Service betaService() {
  return new Service();
 }
}

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.MyConfiguration;
import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {

 private static void printServiceInfo(Service service) {
  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("*******************************************\n\n");
 }

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

  MyConfiguration myConfiguration = applicationContext.getBean(MyConfiguration.class);
  printServiceInfo(myConfiguration.alphaService());
  printServiceInfo(myConfiguration.betaService());

  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 : "15.16.17.18"
Environment : "Development"
Server Ip : "11.21.31.41"
*******************************************


*******************************************
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

Friday, 3 August 2018

Spring boot: @EnableConfigurationProperties example

@EnableConfigurationProperties annotation is used to enable support for @ConfigurationProperties annotated beans.

Example
@Configuration
@EnableConfigurationProperties({AlphaService.class, BetaService.class})
public class MyConfiguration {

}

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;

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;
 }

}

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

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

@ConfigurationProperties(prefix="alpha")
public class AlphaService extends Service{
 
}

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

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

@ConfigurationProperties(prefix = "beta")
public class BetaService extends Service {

}

MyConfiguration.java

package com.sample.myApp.model;

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

@Configuration
@EnableConfigurationProperties({AlphaService.class, BetaService.class})
public class MyConfiguration {

}

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="15.16.17.18"
alpha.servers[0].serverEnvironemnt="Testing"
alpha.servers[1].serverIp="11.21.31.41"
alpha.servers[1].serverEnvironemnt="Development"

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.AlphaService;
import com.sample.myApp.model.BetaService;
import com.sample.myApp.model.Server;
import com.sample.myApp.model.Service;

@SpringBootApplication
public class Application {

 private static void printServiceInfo(Service service) {
  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("*******************************************\n\n");
 }

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

  AlphaService alphaService = applicationContext.getBean(AlphaService.class);
  BetaService betaService = applicationContext.getBean(BetaService.class);

  printServiceInfo(alphaService);
  printServiceInfo(betaService);

  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 : "15.16.17.18"
Environment : "Development"
Server Ip : "11.21.31.41"
*******************************************


*******************************************
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