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

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
 
(未显示同一用户的2个中间版本)
第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>会报错:<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>

2023年1月26日 (四) 10:18的最新版本

首先,因为 @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);
        }
        
    }

}

会报错:

……
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]
……


可以作为对象的属性注入

application.yml

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


Args.java

@Component
@ConfigurationProperties(prefix="args")
@Data
public class Args {

    private String[] include;

}

Application.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);
        }

    }

}