如何从属性文件中注入 spring 中的字符串数组列表?
How to inject List of String arrays in spring from properties file?
如何在应用程序属性的列表中创建嵌套数组,并将其作为值插入?
@Value("${LIST_OF_NESTED_ARRAYS}")
List<String[]> list;
如果您使用的是application.yml
,您可以按如下方式进行:
yourProperty:
- - "Array1 - String1"
- "Array1 - String2"
- - "Array2 - String1"
- "Array2 - String2"
是的,通过使用Spring表达式语言可以实现。
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
用semi-column(;)
分隔上面property
中每个数组的值,在@value
注解里面的split
函数中给出相同的semi-column(;)
.
在下面的输出日志中验证注入列表的输出。
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
如何在应用程序属性的列表中创建嵌套数组,并将其作为值插入?
@Value("${LIST_OF_NESTED_ARRAYS}")
List<String[]> list;
如果您使用的是application.yml
,您可以按如下方式进行:
yourProperty:
- - "Array1 - String1"
- "Array1 - String2"
- - "Array2 - String1"
- "Array2 - String2"
是的,通过使用Spring表达式语言可以实现。
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
用semi-column(;)
分隔上面property
中每个数组的值,在@value
注解里面的split
函数中给出相同的semi-column(;)
.
在下面的输出日志中验证注入列表的输出。
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