6

I am using Spring boot for my application written on Kotlin. I am able to get command line arguments using Environment.getProperty("nonOptionArgs", Array<String>::class.java)

However, inside BeanFactoryPostProcessor i cannot autowire environment - as this post-processor is running too early in the lifecycle. How I can access command line arguments inside BeanFactoryPostProcessor?

Oleksandr Papchenko
  • 2,071
  • 21
  • 30
  • @ Lino you are right - this is Kotlin : ) – Oleksandr Papchenko Jan 15 '19 at 15:00
  • Try to autowire `org.springframework.boot.ApplicationArguments` and then call its method `getSourceArgs`. See if this works. – VHS Jan 15 '19 at 15:12
  • @VHS does not work - when `BeanFactoryPostProcessor` is running - there are no beans at all in context - only bean definition. – Oleksandr Papchenko Jan 15 '19 at 15:20
  • why you need to get command line args in BeanFactoryPostProcessor ? – Abdelghani Roussi Jan 15 '19 at 15:44
  • @Abdelghani Roussi, i would like to define dynamically beans based on command argument values. Why to i do this in `BeanFactoryPostProcessor` - is to be sure that bean definitions are there before actual bean instantiation- so i don't need `@DependsOn` annotation. – Oleksandr Papchenko Jan 15 '19 at 15:48
  • 1
    If there are discreet number of cases based on your command line args, you can try to use [ConditionalOnProperty](https://stackoverflow.com/a/39858222/5749570). – VHS Jan 15 '19 at 15:59
  • 1
    @OleksandrPapchenko in term of loading beans conditionally (like auto-configuration in spring boot), I would say that it's much cleaner to use the `@ConditionalXXX` annotations – Abdelghani Roussi Jan 15 '19 at 16:04

2 Answers2

3

Well , you can implement your BeanFactoryPostProcessor with EnvironmentAware to get the 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;
    }

}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Wow, thank you, this actually works! This is amazing. As i understand there is still no Environment bean defined on the context - at the time of `BeanFactoryPostProcessor` there are no beans at all - however, spring creates Environment really early in lifecycle ? – Oleksandr Papchenko Jan 15 '19 at 16:39
  • Yeah, it creates `Environment` at the constructor when creating the context . My guess is that it will create `Environment` first but later register this instance as a bean .So technically , the `Environment` set into `BeanFactoryPostProcessor` is still not a spring bean yet but it will eventually be a spring bean.. My guess... – Ken Chan Jan 15 '19 at 16:48
1

From your comment :

I would like to define dynamically beans based on command argument values. Why to i do this in BeanFactoryPostProcessor - is to be sure that bean definitions are there before actual bean instantiation- so i don't need @DependsOn annotation.

In term of loading beans conditionally (like auto-configuration in spring boot), I would say that it's much cleaner to use the @ConditionalXXX annotations, most specifically the @ConditionalOnProperty.

Referencing the Java-doc for @ConditionalOnProperty here they said :

Conditional that checks if the specified properties have a specific value. By default the properties must be present in the Environment and not equal to false. The havingValue() and matchIfMissing() attributes allow further customizations.

So you can do something similar to :

@ConditionalOnProperty(prefix = "my.env", name = "var", havingValue = "true", matchIfMissing = false)
Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39