How to alter Spring Bean initialization order? - java

I have an annotation-configured Spring web app, I need to alter bean creation order to define what is the very first bean loaded during context boot up.
Is it possible without switching to xml configuration?

Related

Spring Beans doesn't persist in the session in the first calll

I use spring 5.2.10 for dependecies injection with spring mvc. In the first call from client no beans persist in session. Each time i get a bean from the context during the first call from client, Spring creates it again. It seams that Spring doesn't update the beans repository after create one of them.
It´s more, if i try to retrive a bean just after setting it the result is null.
Why did the bean disapear after having set it?
Thanks

Reload Spring Bean Singleton Instance

Recently, I came into the situation where I needed to reload the Spring Bean instance. Because this bean is using an external yml file which updates sometimes. The requirement is to reinitialize the bean instance after any change in the YML file.

Can you use scoped proxies with Spring Boot 2 #ConfigurationProperties?

Using Spring Boot 1.5.12, we create scoped proxies for #ConfigurationProperties beans. We do this so that we can effectively have property values that are scoped to the user/session/etc. We use a BeanFactoryPostProcessor to register a scoped proxy created by ScopedProxyUtils.createScopedProxy and change the scope of the target bean definition to our custom scope.
This worked great in Spring Boot 1.5.12. However, in Spring Boot 2, the introduction of the Binder API has made this stop working. This is because when Spring Boot binds #ConfigurationProperties in its ConfigurationPropertiesBindingPostProcessor, it uses Bindable.of(type).withExistingValue(bean) passing in a type (e.g. org.example.Foo) and the bean which is an instance of ScopedProxyFactoryBean. Bindable checks that bean is an instance of type which it's not and things blow up.
Does anyone know of a general way to accomplish this? Specifically, to replace the #ConfigurationProperties bean definition with a proxy while still allowing the properties to bind to the instance? I've considered delaying the creation of the proxy until after the target has already been bound by ConfigurationPropertiesBindingPostProcessor (i.e. in my own BeanPostProcessor). However, at this point the bean is instantiated and my proxy can only replace the bean. It can't really leave the original bean in the context as a target. So I'm struggling with how to do this using a BeanPostProcessor.
EDIT: I've created a simple example project that demonstrates the issue (with the ability to check out code that works on Spring Boot 1 and fails on Spring Boot 2).

Reloading class/module at runtime in Spring MVC

I have an existing MVC web application.
Part 1: Is it possible to reload a bean with some changes to the source code with out refreshing the application context?
or,
If I load a class using my custom class loader, will that be managed by spring container? Or can I do anything that it could be managed by Spring container?
Part 2: Is it possible to copy field values from old bean to new bean during class reloading?

How to create a bean in spring that is available to all users in my app?

I'm building an online chat application in spring just like the one on Facebook. I want to create a bean with a property[Array] called active-users. Then performs the following:
Whenever a user logs in, I'll add his/her userId into the array.
When an other user logs in, I'll display the users that are
currently online.
How do I create a bean which is available at all times?
For Ex : In servlets, this can be achieved by using the Servlet context :
ServletContext context = request.getServletContext();
context.setAttribute("userId", "123");
All beans in Spring are singletons by default, they will be alive during the whole application's lifecycle unless you'll do something with the spring context.
So just create a spring bean and declare a global list in it. You can access it anywhere where the spring bean will be injected from the current context.
It's simpler with Spring than in a pure servlet application, because all beans declared in root application context reside in fact automatically in the ServletContext and as such are unique in the application. And Spring can natively inject them in any controller or service bean to allow you to use them at will.
The only limit, is that they are unique per instance, so it won't be enough it you had a farm of servers for your application.

Categories

Resources