Home > Back-end >  List of Strings arrays as a value in spring
List of Strings arrays as a value in spring

Time:11-02

how to create a nested arrays in list in application properties, and insert it as a value?

@Value("${LIST_OF_NESTED_ARRAYS}")
List<String[]> list;

CodePudding user response:

If you are using application.yml you can do it as follows:

yourProperty:
 - - "Array1 - String1"
   - "Array1 - String2"
 - - "Array2 - String1"
   - "Array2 - String2"

CodePudding user response:

Yes, By using Spring Expression Language it can be achieved.

Code:

package com.test.listofarray;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@Slf4j
public class TestApplication implements CommandLineRunner {

    @Value("#{'${LIST_OF_NESTED_ARRAYS}'.split(';')}")
    private List<String[]> list;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);

    }

    @Override
    public void run(String... args) throws Exception {

        list.forEach(array -> log.info("array ----> {}", Arrays.toString(array)));
    }

}

application.properties

LIST_OF_NESTED_ARRAYS=India,USA,Brazil;Asia,Africa

use semi-column(;) to separate the values of each array in the above property and give the same semi-column(;) in the split function inside @value annotation.

Verify the output of the injected list in the below output log.

Output:

2021-11-01 17:48:39.105  INFO 11352 --- [           main] com.resilience.retry.RetryApplication    : No active profile set, falling back to default profiles: default
2021-11-01 17:48:42.184  INFO 11352 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-01 17:48:42.206  INFO 11352 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-01 17:48:42.207  INFO 11352 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-11-01 17:48:42.411  INFO 11352 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-01 17:48:42.411  INFO 11352 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3235 ms
2021-11-01 17:48:45.139  INFO 11352 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-11-01 17:48:45.256  INFO 11352 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-01 17:48:45.285  INFO 11352 --- [           main] com.resilience.retry.RetryApplication    : Started RetryApplication in 6.946 seconds (JVM running for 7.648)
2021-11-01 17:48:45.397  INFO 11352 --- [           main] com.resilience.retry.RetryApplication    : array ----> [India, USA, Brazil]
2021-11-01 17:48:45.400  INFO 11352 --- [           main] com.resilience.retry.RetryApplication    : array ----> [Asia, Africa]
2021-11-01 17:49:03.860  INFO 11352 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-01 17:49:03.861  INFO 11352 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-01 17:49:03.862  INFO 11352 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
  • Related