Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2 - @Component loading before @Configuration

I have a Spring Boot Application and two classes that come from different jars that i am using, where one of them is @Component, and the other one is @Configuration.
Both of them have @PostConstruct methods and basically here is my use case -> i want the @Configuration's @PostConstruct to run before @Component's @PostConstruct. Is it possible to achieve this somehow?
I tried with @DependsOn on the @Component (referencing the @Configuration - which does not have any beans inside - only @PostConstruct), but it does not work.
Here are code pieces.
First file:

@Configuration
public class MainConfig {
    @PostConstruct
    public void postConstruct() {
        // doSomething
    }
}

Second file.

@Component
public class SecondClass {
  @PostConstruct
  public void init() throws InterruptedException {
    // doSomething that depends on postConstruct from MainConfig
  } 
}

Thanks a lot in advance

like image 489
sem10 Avatar asked Feb 26 '26 21:02

sem10


1 Answers

@Configuration
public class MainConfig {

    public void postConstruct() {
        // doSomething
    }
}


@Component
public class SecondClass {

  @Autowired
  private MainConfig mainConfig;

  @PostConstruct
  public void init() throws InterruptedException {
    mainConfig.postConstruct();
    // doSomething that depends on postConstruct from MainConfig
  } 
}
like image 111
Mykhailo Skliar Avatar answered Feb 28 '26 11:02

Mykhailo Skliar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!