查看“Application.yml参数注入到数组”的源代码
←
Application.yml参数注入到数组
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== 首先,因为 @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>会报错:<syntaxhighlight lang="console"> …… Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'include' in value "${include}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[spring-core-5.3.1.jar:5.3.1] at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.3.1.jar:5.3.1] at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.3.1.jar:5.3.1] …… </syntaxhighlight> === 可以作为对象的属性注入 === ==== application.yml ==== <syntaxhighlight lang="yaml"> args: include: - E:\record\2022 - E:\record\2023 </syntaxhighlight> ==== Args.java ==== <syntaxhighlight lang="java"> @Component @ConfigurationProperties(prefix="args") @Data public class Args { private String[] include; } </syntaxhighlight> ==== Application.java ==== <syntaxhighlight lang="java"> @SpringBootApplication @Slf4j @Data public class Application implements CommandLineRunner { @Autowired Args args; 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) { log.info("EXECUTING : command line runner"); for (String str:this.args.getInclude()) { log.info(str); } } } </syntaxhighlight>
返回至
Application.yml参数注入到数组
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息