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?
Related
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.
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 !
I want to gradually transition Spring XML configurations into Java configurations. Will there be any issues having the same beans definitions in both the XML and Java #Configuration?
When a duplicate bean is detected (by bean id) during the bean definition phase, it replaces the definition of the previous bean. If you have identical bean definitions (XML and java config), then it should have no impact to you. As you are moving to Java Config, it would be important to load any duplicate XML beans first, so that you are confident that the Java Config bean is the last one loading, and the one used to build the bean. If the XML bean was loading second, you could not be sure that the Java Config bean was set up correctly (as it was replaced by XML).
EDIT: I created a project which was based on XML, and then added #Configuration based beans. My observation was that Spring has no problem with duplicate bean definitions (by id), however in the short time I tried, the XML bean definition overrode the java config #Bean. It is up to you if you want to attempt to get #Configuration beans to override XML Beans.
I am configuring my spring application per-environemnt and I came to following problem:
As long as the environment changes just bean constructor/properties values I am fine with using PropertyPlaceholderConfigurer.
Now I am facing the fact that the bean structure itself change in different environemnts. E.g. in test environemnt I define single bean where in production environment I define another bean of same interface which requires property of type List set - in another words different structure where PropertyPlaceholderConfigurer can't really help.
I went with defining per-environment spring xml configuration importing it via <import resource="myDefinition-${Environment}.xml />. This is also fine until I want have it optional. The resource I am defining there is #Autowired(required = false) to another bean.
Since <import ... /> doesn't allow optional attribute (as can be seen here: https://jira.springsource.org/browse/SPR-1624) I ended up having empty .xml configuration files for environemnts where I don't require having that bean. This is somewhat inconvenient.
Could anyone advice on best practice in such scenario?
Thanks.
Bean definition profiles, introduced in Spring 3.1 are designed to solve just this kind of problem. See http://static.springsource.org/spring/docs/3.1.0.RC2/spring-framework-reference/html/new-in-3.1.html
I am using Java-based Spring configuration in my project, specifying bean construction in #Bean-annotated methods in #Configuration. Recently, Recently, I've started to think that maybe it would've been better to use #Autowired to remove all non-important beans from #Configuration, leaving only small "root" set of them (key services and technical beans like those of Spring MVC).
Unfortunately, it seems that Spring can notice implementations for #Autowired dependencies only if they are inside component-scanned package which I cannot do without resorting to some XML.
Is there any way to use #Autowired with Java-based configuration without explicitly specifying each bean?
If I understand you correctly, you're expecting Spring to auto-discover the DaoImpl class based on the autowired dependency on the Dao interface.
This isn't going to happen - you either need to use component scanning, or you need to explicitly declare the bean, either as <bean> or #Bean.
The reason for this is that Java provides no mechanism to discover classes which implement a given interface, the classloader just doesn't work that way.
If you are implementing the Idao via dao and you are looking to #Autowire that dependency into your reference var... you need to first:
define the bean so you (in Java Based Config) simply return the impl class to the interface. The bean name is that of your method name.
When you autowire this, it will search for a matching name between your reference variable you are looking to autowire and your declaration.
THEN you will be fine. Hope this helps.