Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding of bean coming from external library

Tags:

java

spring

If I have a project in which I have a dependency on a library in which a bean’s scope is unspecified, meaning singleton by default, is there a way to override the bean definition to keep it the same but only change the scope to prototype, either at runtime or through bean configuration?

like image 720
Dimitre Bogdanov Avatar asked Oct 20 '25 16:10

Dimitre Bogdanov


1 Answers

There are three cases:

  • If bean which you want to override is in configuration class marked with @Confgiuration annotation , you can exclude this configuration class and declare all beans from there
  • If annotation is present directly on class than you should exclude those class from @ComponentScan basePackages .
  • You can set the order of configuration classes marked with @Configuration annotation , the last one configuration class will override all previous declared beans
like image 180
Mykhailo Moskura Avatar answered Oct 22 '25 05:10

Mykhailo Moskura



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!