Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 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 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 ListgetSupportedVersions() {
return supportedVersions;
}

public void setSupportedVersions(List 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.0modelVersion>
groupId>com.sample.appgroupId>
artifactId>springBootAppartifactId>
version>1version>

parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.1.6.RELEASEversion>
parent>

dependencies>

dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring boot: @Value: Populate list from a property

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×