使用自定义参数(如文件)调用Spring PropertySourcesPlaceholderConfigurer

时间:2019-01-16 10:58:56

标签: java spring spring-boot

当我尝试运行我的spring boot项目时,出现如下错误:

说明:

  

方法propertySourcesPlaceholderConfigurer中的参数0   AppConfig需要类型为'java.io.File'的bean   找不到。

操作:

Consider defining a bean of type 'java.io.File' in your configuration.

@SpringBootApplication
@Slf4j
public class AppRunner implements CommandLineRunner {

    @Autowired
    private BeanFactory beanFactory;

    public static void main(String[] args) throws IOException {
        SpringApplication.run(AppRunner.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        File file = new File("path\\credentials.properties");
        PropertySourcesPlaceholderConfigurer report =
                beanFactory.getBean(PropertySourcesPlaceholderConfigurer.class, file);
    }
}

我的配置文件如下:

@Configuration
public class AppConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(File file) throws IOException {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setProperties(Utils.getProperties(file));
        propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
        return propertySourcesPlaceholderConfigurer;
    }
}

我想用参数调用单例bean。但我尝试这样做,却收到上面定义的错误。我该怎么解决?

2 个答案:

答案 0 :(得分:0)

使用PropertySourcesPlaceholderConfigurer的意图是什么?您已经创建了Bean,因此可以通过@Autowired注入它。

带有@Bean批注的方法是在应用程序启动时通过Spring调用的。如果要手动初始化该bean,则必须删除@Bean批注或在AppConfig类中创建基于文件的bean:

@Bean
public File getFile() {
    return new File("path\\credentials.properties");
}

编辑:

如果您要在创建带有@Bean批注的bean时使用命令行值,请看一下这篇文章:Spring Boot: get command line argument within @Bean annotated method

答案 1 :(得分:0)

问题根据@M的评论解决。 Deinum如下:

@SpringBootApplication
@Slf4j
public class AppRunner implements CommandLineRunner {

    public static void main(String[] args) throws IOException {
        System.setProperty("spring.config.additional-location","path\\credentials.properties");
        SpringApplication.run(AppRunner.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        ...
    }
}

或使用环境:

@Configuration
public class AppConfig  implements EnvironmentAware {

    Environment env;

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        Properties properties = Utils.getProperties(new File(env.getProperty("credential")));
        properties.setProperty("startDate",env.getProperty("startDate"));
        propertySourcesPlaceholderConfigurer.setProperties(properties);
        propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
        return propertySourcesPlaceholderConfigurer;
    }

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;
    }
}