我一直很高兴地使用@Value
将命令行参数注入Spring Boot基于CommandLineRunner的程序中。即Spring引导中的类型转换CommandLineRunner应用程序
java -jar myJar.jar --someParm=foo
...只是罚款含类:
@Autowired
public MyBean(@Value("someParm") String someParm) { ... }
但是我现在看到当PARM不是一个String此失败。
这里是我的豆:
@Component
class MyBean {
private final LocalDate date;
@Autowired
public MyBean (@Value("date") @DateTimeFormat(iso=ISO.DATE) LocalDate date) {
this.date = date;
}
public void hello() {
System.out.println("Hello on " + date);
}
}
...和我的应用程序类:
@SpringBootApplication
public class MyApp implements CommandLineRunner {
@Autowired
private MyBean myBean;
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Override
public void run(String... args) throws IOException {
myBean.hello();
}
}
当我运行它为java -jar MyApp.java --date=2016-12-10
,我得到结束堆栈跟踪:
java.lang.IllegalStateException: Cannot convert value of type
[java.lang.String] to required type [java.time.LocalDate]:
no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
尽管文档声明String-> Date有一个标准转换器,但我已经试用了registeri ng我自己的,并且碰到与此帖子相同的NullPointerException:How to register custom converters in spring boot?
我能做些什么来完成这项工作?
的Java 8,春季启动1.3.5-RELEASE
我曾尝试 - 除了类型外,同样的例外情况。 – slim