“Application.yml参数注入到数组”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
第1行: 第1行:
1
+
=== 首先,因为 @Value 的实现机制,下面这种注入是不支持的 ===
 +
 
 +
==== application.yml ====
 +
<syntaxhighlight lang="yaml">
 +
include:
 +
  - E:\record\2022
 +
  - E:\record\2023
 +
</syntaxhighlight>
 +
 
 +
==== Application.java ====
 +
<syntaxhighlight lang="java">
 +
@SpringBootApplication
 +
@Slf4j
 +
@Data
 +
public class Application implements CommandLineRunner {
 +
 
 +
    @Value("${include}")
 +
    String[] include;
 +
 
 +
    public static void main(String[] args) {
 +
        log.info("STARTING THE APPLICATION");
 +
        SpringApplication.run(Application.class, args);
 +
        log.info("APPLICATION FINISHED");
 +
    }
 +
 
 +
    @Override
 +
    public void run(String... args) {
 +
 
 +
        for (String str:include) {
 +
            log.info(str);
 +
        }
 +
       
 +
    }
 +
 
 +
}
 +
</syntaxhighlight>

2023年1月26日 (四) 10:14的版本

首先,因为 @Value 的实现机制,下面这种注入是不支持的

application.yml

include:
  - E:\record\2022
  - E:\record\2023

Application.java

@SpringBootApplication
@Slf4j
@Data
public class Application implements CommandLineRunner {

    @Value("${include}")
    String[] include;

    public static void main(String[] args) {
        log.info("STARTING THE APPLICATION");
        SpringApplication.run(Application.class, args);
        log.info("APPLICATION FINISHED");
    }

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

        for (String str:include) {
            log.info(str);
        }
        
    }

}