wire多个配置类

lyfkaqu1  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(376)

我在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) ,同样的结果,它失去了注射点。我是不是用错了这些注解?谢谢

n3h0vuf2

n3h0vuf21#

我把构造器换成了 BwlConfiguration 使用:

@Bean
public String address(){
    return conf.get(SPI_BL);
}

相关问题