how to relinitialize a singleton bean in springboot - spring-boot

I am creating a bean of list of pojo. However I want to make this bean re-initilaize in each and every execution of method in which this bean is used. Is there a way I can achieve the same in spring-boot.

Related

Is there a way to mark a bean as not primary in Spring?

I am having two beans in different packages out of which one of them is a library so cannot edit, and execution gives the
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type '' available:
For current purpose I want the bean in the library to be the primary bean, is there a way to avoid using the second bean which i can edit , without deleting the bean?
So far have only seen the #Primary annotation
when spring is trying to inject the dependency it found to to beans for it .
so you have add #Qualifier annotation and give the corresponding class in it which you want to inject

Spring Dependency injection, singleton bean injection

In Spring Dependency injection, if a prototype bean is injected in a singleton bean and after declaring the applicationcontext i am calling the getbean method for the singleton class object, then how many new instances are created for the prototype bean inside the singleton object?
Spring creates new instance of prototype bean every time it needed (for each #Autowired in application context). So in your case Spring creates only one instance of prototype (to inject it into a singleton bean).

Does an Object in Spring created every time we write getBean()?

I have read that whenever we do getBean() in spring, it returns desired object.
So does it mean, if i write call getBean() 1000 times, thousand object will be created ??
If yes, Than how Spring manages these objects ?
if No, Please explain how Spring works with respect to object creation ?
Is there something Object pool kind of concept ?
Please clarify my doubts. I am new to spring and is very confused whether spring framework is created to make our task easy or to make things more complicated.
Spring seems to be a web of XMLs :(
From the Spring Framework documentation on singleton bean factory scope:
The singleton scope
Only one shared instance of a singleton bean is managed, and all
requests for beans with an id or ids matching that bean definition
result in that one specific bean instance being returned by the Spring
container.
To put it another way, when you define a bean definition and it is
scoped as a singleton, the Spring IoC container creates exactly one
instance of the object defined by that bean definition. This single
instance is stored in a cache of such singleton beans, and all
subsequent requests and references for that named bean return the
cached object.
To sum it up, no, Spring will create only a single instance of each bean in a bean factory unless you change the default scope of singleton to some other bean scope.
Other bean scopes include:
Bean scopes
singleton (Default) Scopes a single bean definition to a
single object instance per Spring IoC
container.
prototype Scopes a single bean
definition to any number of object instances.
request Scopes a single bean definition to the
lifecycle of a single HTTP request; that is, each HTTP request has its
own instance of a bean created off the back of a single bean
definition. Only valid in the context of a web-aware Spring
ApplicationContext.
session Scopes a single
bean definition to the lifecycle of an HTTP Session. Only valid in the
context of a web-aware Spring ApplicationContext.
global session Scopes a single bean definition to the
lifecycle of a global HTTP Session. Typically only valid when used in a portlet
context. Only valid in the context of a web-aware Spring
ApplicationContext.

Spring register ApplicationListener bean at runtime

I am using
applicationContext.getAutowireCapableBeanFactory().initializeBean(bean, name);
to register a bean at runtime. This bean could actually be considered a child bean of an existing singleton.
If the bean implements ApplicationListener, I get a warning in the log, since there is no underlying bean definition to indicate that this bean should be considered a singleton. And of course it doesn't receive application events, like the warning says.
Inner bean 'name' implements ApplicationListener interface but is not reachable for event multicasting by its containing ApplicationContext because it does not have singleton scope. Only top-level listener beans are allowed to be of non-singleton scope.
How can I register the bean as singleton at runtime, so I avoid the bean post processor warning, as well as have application events work?
(See AbstractApplicationContext:1413)

How can i remove a singleton spring bean from ApplicationContext?

I want to develop a module control system so that every spring bean can be managed by my own LifeCycle Controller.
But I can not figure out how can I remove a singleton spring bean out of ApplicationContext.
That may be an interesting problem , can you help me to resolve ?
Removing definition does both : removing definition and destroying (removing all container references on that bean) corresponding Singleton :
((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("myBean");
If you just need to remove the singleton then :
((DefaultListableBeanFactory) beanFactory).destroySingleton("myBean");
The latter way may be especially useful if you just registered singleton but haven't defined any bean definitions, i.e.
((SingletonBeanRegistry) beanFactory).registerSingleton("myBean", myBeanInstance);
You can try removing the bean definition. Get the BeanDefinitionRegistry and call removeDefinition(..)
It depends on the way you create your application, but for example in web application you can get the definition registry by:
BeanDefinitionRegistry factory =
(BeanDefinitionRegistry) applicationCtx.getAutowireCapableBeanFactory();
(the bean factory implements BeanDefinitionRegistry).
I don't know if the bean instance will be removed as well. Give it a try.

Resources