如何在 BeanFactoryPostProcessor 中获取命令行参数?

Ole*_*nko 5 spring kotlin spring-boot

我正在将 Spring boot 用于我在 Kotlin 上编写的应用程序。我能够使用获取命令行参数Environment.getProperty("nonOptionArgs", Array<String>::class.java)

但是,在BeanFactoryPostProcessor我无法自动装配环境中 - 因为此后处理器在生命周期中运行得太早。我如何访问里面的命令行参数BeanFactoryPostProcessor

Ken*_*han 3

好吧,你可以实现你的BeanFactoryPostProcessorwithEnvironmentAware来获得Environment

@Component
public class FooBeanFactoryPostProcessor implements BeanFactoryPostProcessor , EnvironmentAware{

    private Environment env;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

            env.getProperty("nonOptionArgs");
            //I should be able to access env at here .Hehe

    }

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

}
Run Code Online (Sandbox Code Playgroud)