Spring Tips: Programmatic Bean Registration in Spring Framework 5.0
Vložit
- čas přidán 5. 07. 2024
- Speaker: Josh Long
/ starbuxman
Hi Spring fans! in this installment, we'll look at different ways to register beans with Spring, culminating in the new programmatic bean registration functionality in Spring 5. - Věda a technologie
Hey Josh, nice presentation as usual. I was hoping there was going to be new support for switching bean implementations at runtime, after the application has been running for a while, e.g. in response to some change in the environment. What's the best way to do that please?
Would feature toggles be useful in such situations?
Yes, my question to Josh was more about how to get the Spring application context to start providing one implementation of some interface instead of another, e.g. at startup you might register a NoOpPersonServiceImpl under the PersonService interface, then in response to some environmental change, switch that bean to be backed by some other implementation, e.g. a DatabaseBackedPersonServiceImpl.
Nice feature, is it possible to use the registerBean while the app is running via the reference to the applicationContext
Yes this is possible with a handle to the bean factory. However the context would need to be refreshed for any effect at runtime. This needs to be carefully and infrequently if not rare.