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

No comments:

Post a Comment