Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How to get command line argument inside BeanFactoryPostProcessor?

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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;
    }

}
over 4 years ago · Santiago Trujillo Report

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!