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

No comments:

Post a Comment