我在Spring上配置课。我尝试用依赖注入的配置Map来替换硬编码的字符串。
@Configuration
@Component
public class BwlConfiguration {
@Resource(name="loadParameters")
private Map<ConfigEnum, String> conf;
private String address;
public BwlConfiguration() {
address = conf.get(SPI_BL);
}
...
}
提供confMap的类:
@Configuration
@Component
public class ConfigLoader {
@Resource(name="returnEnv")
private Map<String, String> env;
@Bean
public Map<ConfigEnum, String> loadParameters() throws ParameterNotSetException{
....
return parameterMap;
}
提供环境Map的类:
@Configuration
public class EnvConf {
@Bean
public Map<String, String> returnEnv(){
return System.getenv();
}
}
当我运行程序时,nullpointerexception被抛出 address = conf.get(SPI_BL);
线路。我试着替换 @Component
由 @Import(...class)
,同样的结果,它失去了注射点。我是不是用错了这些注解?谢谢
1条答案
按热度按时间n3h0vuf21#
我把构造器换成了
BwlConfiguration
使用: