First you make your abstract configuration, which is achieved not , denoting it as @Configuration , for example:
// notice there is no annotation here public class ParentConfig { @Bean public ParentBean parentBean() { return new ParentBean(); } }
Then you expand it, for example:
@Configuration public class ChildConfig extends ParentConfig { @Bean public ChildBean childBean() { return new ChildBean(); } }
The result will be the same as if you did this:
@Configuration public class FullConfig { @Bean public ParentBean parentBean() { return new ParentBean(); } @Bean public ChildBean childBean() { return new ChildBean(); } }
Edit : Answer the next question in a comment.
If Spring selects both classes, parent and child, there are problems with duplicate beans, so you cannot distribute it directly . Even if you override the methods, beans from the superclass will also be instantiated by ParentConfig .
Since your parent class is already compiled , you have 2 options:
To clarify solution 2:
If the parent class is in the com.parent.ParentConfig package and the child class is the com.child.ChildConfig package, you can configure component scanning to only get classes under com.child .
You can specify component scan packages using the @ComponentScan("com.child") annotation @ComponentScan("com.child") in the main configuration file (assume the application class).
Esala source share