我在春天有工作配置类。我尝试使用依赖注入替换配置映射的硬编码字符串。
@Configuration
@Component
public class BwlConfiguration {
@Resource(name="loadParameters")
private Map<ConfigEnum, String> conf;
private String address;
public BwlConfiguration() {
address = conf.get(SPI_BL);
}
...
}
提供conf map的类:
@Configuration
@Component
public class ConfigLoader {
@Resource(name="returnEnv")
private Map<String, String> env;
@Bean
public Map<ConfigEnum, String> loadParameters() throws ParameterNotSetException{
....
return parameterMap;
}
提供env map的类:
@Configuration
public class EnvConf {
@Bean
public Map<String, String> returnEnv(){
return System.getenv();
}
}
当我运行程序时,会在address = conf.get(SPI_BL);
行抛出nullPointerException。我尝试将@Component
替换为@Import(...class)
,结果相同,而且它失去了注射点。
我使用这些注释错了吗?感谢
答案 0 :(得分:0)
我用BwlConfiguration
替换了构造函数:
@Bean
public String address(){
return conf.get(SPI_BL);
}