[英]spring batch using spring boot: Read arguments from config or command line and use them in job
[英]how to read values from command line and use them in bootstrapped annotation config class to define spring bean?
我的主要課程如下。
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
SimpleCommandLinePropertySource cmdLineSrc = new SimpleCommandLinePropertySource(args);
ctx.getEnvironment().getPropertySources().addFirst(cmdLineSrc);
ctx.register(MyConfig.class);
ctx.refresh();
ctx.start();
MyClass myclass = ctx.getBean(MyClass.class);
// do something...
}
現在在我的MyApp.config中,我有以下內容。
@Autowired
Environment env;
但是,當我嘗試獲取價值時,我得到了NPE。
env.getProperty("something");
我在這里想念什么? 我確實有如下所示的@PropertySource(效果很好),但是這可能會引起任何問題嗎?
@Configuration
@PropertySource({ "classpath:my.properties" })
public class MyConfig {
@Autowired
private Environment env;
@PropertySource聲明覆蓋了我的@Configuration類中的環境變量。 我刪除了它,並能夠檢索在命令行中輸入的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.