Spring Bean Creation Order - java

Is there a way to find out bean creation order (or at least dependency order) after Spring context has been refreshed?
Just a simple order of bean definitions would do it. I dont want the order in which they were registered. I want the order in which they will be created (dependency tree)

Adding a postProcessor can resolve this. Not an awesome solution but works.
After context is up there is no way to find out in which order beans were crated. Unless you use spring-dependent external lib for finding out relationships between bean definitions.

Related

Do action before spring context created

How can I execute the action, before Spring context created? I found only one solution that more or less can satisfy me, it is listening ApplicationStartingEvent but I don't know how correctly do it because Spring doesn't see listener bean because context not created yet. So maybe someone knows ways how to catch ApplicationStartingEvent or maybe another better solution to do it.
Interface ApplicationContextInitializer could suffice for your requirement.
Read ApplicationContextInitializer documentation
Implementing ApplicationContextInitializer allows you to do additional tasks/initializations before persistent bean definition is loaded (i.e. your application-context.xml). One such use is, when you want to select profiles before you will load definitions.

Bean and Dependency Injection configuration from Database Instead XML

Currently we have all service class configuration defined in Application-Context.xml file. Application context will be initialized during the application startup with all beans defined in the context file and spring handles dependency injection.
I am looking for a solution where it has to load particular service class during run time based on specific parameter from Database.
For example, there are two classes exist in code base such as FooService1.java and FooService2.java. Each class will have dependency with appropriate DAO class such as FooDAO1.java and FooDAO2.java.
Instead of defining these in applciation-context.xml file, a run time parameter will decide which service needs to be loaded and its corresponding DAO which needs to be injected. Basically what i am trying to achieve here is DB oriented Dependency Injection to keep all application context information in database instead of XML.
Tables would look like: SERVICE_BEANS, DAO_BEANS and some intermediate table to have dependency information.
I just saw JdbcBeanDefinitionReader class in spring. Can I use this to implement DB oriented DI? I don't see much example on this. Please let me know if anyone has any examples.
I think you can generate beans # runtime and register them to spring-context.
Refer to Registering beans(prototype) at runtime in Spring
Hope that helps !

Analysing spring bean definitions

Our application exports its configuration as several sets of Spring beans.
I have no control over how it does this.
I want to write something that documents the dependencies between the configuration items defined in these beans. Note: these are dependencies at the application level, nothing to so with Spring dependencies (so we might have configuration items of type Actresses who have a dependency on certain items from the type Bishops, but - at the Spring bean level - this is merely that the value of a property in the Actress matches the value of a different property of the Bishop).
So I'd like to use some library or toolset that lets me load up a set of bean definition XML, iterate over them and the content of each, extract property values and so on.
From some googling, I can find ways of extending the parsing Spring does itself, but I don't want that - I want something I can run offline outside of the Spring-using app itself.
Can someone point me at some resources for doing this?
The closest thing I've heard of to this is Spring BeanDocs:
http://spring-beandoc.sourceforge.net/

Spring Tool Suite - Search for bean declaration by id OR alias

I'm using Spring Tool Suite (STS) to manage a large Java Spring project. Is there a function in STS that allows one to find the declaration of a particular bean by its alias?
For example, if I have the following property in a Java class:
#Resource
private WidgetService widgetService;
I know that this property will be auto-wired from a Spring bean with the name "widgetService". The bean injected will be an instance of a class that implements the interface WidgetService. The implementation(s) may not be located in the same package as the interface. I need a way to quickly determine from that property declaration which implementation will be injected.
I tried searching for widgetService using the Spring Beans Search, but it came up empty. It came up empty because the bean is defined as follows:
<alias alias="widgetService" name="defaultWidgetService" />
<bean id="defaultWidgetService" class="namespace.DefaultWidgetService"/>
There's the answer to my search (DefaultWidgetService), but it's inside a bean named "defaultWidgetService". Since the bean that is used to autowire the #Resource is aliased, the Spring Beans Search doesn't work, as it only has options to search by name, class, etc, but not by alias.
I know I can just use a File Search and search for alias="widgetService" but because of the size of the project searching by text is quite slow. I could also examine the property at debug time and get the class of the injected bean, but that is also cumbersome. Java Search is quick because it is indexed, and Spring Beans Search is quick for the same reason. But I wonder: why I am using STS (which adds extra time to my builds so STS can index the Spring beans) if it won't help me with finding aliased beans?

Spring Load A Bean Before Any Other Bean

I have a requirement that I want to initialize a bean before any other bean in my application context. One way to do is to use "depends-on" on every other bean but its too verbose as I have many beans in my application-context.
Is there any other way to do this. I have read abt implementing ServletContext for this but I dont think this will work as this has nothing to do with Spring.
Please let me know your thoughts on it.
Thanks,
Raunak
Make sure that your bean comes first in your context.xml and it will be instantiated first. You can make a simple test to verify.

Categories

Resources