[英]how to read values from command line and use them in bootstrapped annotation config class to define spring bean?
i have my main class as below. 我的主要课程如下。
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...
}
now in my MyApp.config, i have following.. 现在在我的MyApp.config中,我有以下内容。
@Autowired
Environment env;
however, when i try to get values out, i get NPE. 但是,当我尝试获取价值时,我得到了NPE。
env.getProperty("something");
what am i missing here? 我在这里想念什么? I do have @PropertySource as below (which works fine), but could that cause any issues here? 我确实有如下所示的@PropertySource(效果很好),但是这可能会引起任何问题吗?
@Configuration
@PropertySource({ "classpath:my.properties" })
public class MyConfig {
@Autowired
private Environment env;
@PropertySource declaration was overriding environment variables in my @Configuration class. @PropertySource声明覆盖了我的@Configuration类中的环境变量。 i removed it and i was able to retrieve values i entered in command line. 我删除了它,并能够检索在命令行中输入的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.