Monday 23 March 2020

Spring boot: @Value: Populate list from a property

Suppose you have comma separated values like below.
supportedVersions=0.5,0.7,0.9

You can populate a list from the property 'supportedVersions' using Spring EL (Expression Language).

@Value("#{'${supportedVersions}'.split(',')}")
private List<String> supportedVersions;

Find the below working application.

AppConfig.java
package com.sample.app.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

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

 @Value("${currentVersion}")
 private String currentVersion;

 @Value("#{'${supportedVersions}'.split(',')}")
 private List<String> supportedVersions;

 public String getAppName() {
  return appName;
 }

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

 public String getCurrentVersion() {
  return currentVersion;
 }

 public void setCurrentVersion(String currentVersion) {
  this.currentVersion = currentVersion;
 }

 public List<String> getSupportedVersions() {
  return supportedVersions;
 }

 public void setSupportedVersions(List<String> supportedVersions) {
  this.supportedVersions = supportedVersions;
 }

}

App.java
package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.config.AppConfig;

@SpringBootApplication
public class App {
 
 @Autowired
 private AppConfig appConfig;
 
 public static void main(String args[]) {
  SpringApplication.run(App.class, args);
 }

 @Bean
 public CommandLineRunner demo() {
  return (args) -> {
   
   System.out.println("Application Name : " + appConfig.getAppName());
   System.out.println("Application Version : " + appConfig.getCurrentVersion());
   
   System.out.println("Supported Versions");
   appConfig.getSupportedVersions().stream().forEach(System.out::println);
  

  };
 }

}

Create ‘application.properties’ file under ‘src/main/resources’ folder.

application.properties
name=Chat Server
currentVersion=1.1
supportedVersions=0.5,0.7,0.9

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sample.app</groupId>
 <artifactId>springBootApp</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <dependencies>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

 </dependencies>
</project>

Total project structure looks like below.

Run App.java, you will see below messages in console.

Application Name : Chat Server
Application Version : 1.1
Supported Versions
0.5
0.7
0.9

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment