Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot : configuration inheritance

I have

package com.parent.spring;
public abstract class ParentConfig {

@Bean
public String bParent() {
   return "parent bean";
}

then

package com.child.spring;
public class ChildConfig extends ParentConfig {
  @Bean
  public String bChild() {
     return "child bean";
  }
}

and here's how I am starting my spring boot app

@SpringBootApplication
@ComponentScan(basePackages = { "com.child.spring","com.parent.spring" })
public class MyMain {
    public static void main(String args[]) throws Exception {
        SpringApplication.run(MyMain.class, args);
    }
}

I want to load beans from child config first and the from parent config. However, opposite is happening. Don't want to use depends on option as this is just an example, in reality I have lot of beans in child and parent config. Also, I have bunch of other configuration classes in parent package so I don't want to remove that from component scan. Any nicer solution on this ?

like image 709
Sagar Avatar asked Jul 26 '26 15:07

Sagar


1 Answers

I want to load beans from child config first and the from parent config.

You want to explain to the framework how to do its work ?
Spring Configurations and more generally Spring beans are loaded and added in the context in a order that Spring considers the right to satisfy all dependencies requirements.
Besides, configuration inheritance is not documented as a way to achieve such a thing.
Specifying explicitly the order for specific beans is possible but it should be the exception and not the norm while you seem want to do that exception the norm.

like image 193
davidxxx Avatar answered Jul 30 '26 09:07

davidxxx



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!