ref vs depends-on attributes in Spring - java

I am confused between ref and depends-on attribute in Spring.I read the spring doc but I am still confused.I wish to know the exact difference between the two and in which case which one shall be used.

From the official documentation: http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/DependsOn.html
Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialisation.

Perhaps an example of a situation where depends-on is needed would help. I use Spring to load and wire my beans. Here is an example bean definition:
<bean id="myBean" class="my.package.Class">
<property name="destination" value="bean:otherBeanId?method=doSomething"/>
</bean>
<bean id="otherBeanId" class="my.package.OtherClass"/>
Notice that the property value is a string, which references otherBeanId. Because of the way this variable is resolved, Spring doesn't learn of the dependency, so it may destroy otherBeanId then myBean. This may leave myBean in a broken state for a little while.
I can use depends on to fix this problem as follows:
<bean id="myBean" class="my.package.Class" depends-on="otherBeanId">
<property name="destination" value="bean:otherBeanId?method=doSomething"/>
</bean>

There might be a situation where a bean might be a property in another bean i.e; the property bean is directly involved in the bean definition as a property in such case we refer the beans with ref attribute.
There might be a situation where in a bean instantiation is required for the other bean to be successfully created, the other bean is not a property of the bean under definition, in such case we make use of the depends-on attribute.

Related

Enforce initialization of Spring Bean which's reference is not explicitly used

Is there a way to enforce the initialization of a Spring Bean in cases that the reference of the bean is never explicitly used or even requested in the ApplicationContext?
<bean class="foo.bar.FooBar>
<property name="fooBar" ref="foo.bar.reference"/>
</bean>
This bean is meant to do things inside, get's properties passed by IoC but it is never used by any other bean which implies that the it's reference is nowhere else configured.
My problem is, that this bean seems not to be initialized because of that.
I tried <bean .. lazy-init="false"/> but this did not do the trick.
How can ensure the bean is going to be initialized?
Since I cannot modify the application context, I would need a way doing it just in the XML configuration.
I'm not sure, but maybe you could try to use the singleton=true property for that bean - in non-singleton mode, the bean would be instatiated only if requested by the application.
http://docs.spring.io/spring/docs/1.2.9/reference/beans.html
(see 3.2.5)
Edit: Can you add some logging to ctor to see if it's called? If not, could you add an init-method and log something there?

Spring context:property-placeholder for a boolean value

I am working on an application where I have two classes both implementing a common interface. So in time of bean declaration, I am going to mark one of them primary in my app-context.xml file. I can achieve this by simply declaring the primary bean like this:
<bean id="oracleImpl" class="com.me.dao.OracleImpl" primary="true">
</bean>
Now I don't want to hard code which of the beans is going to be the primary bean, rather want to read the true/false value from a properties file. So I went like this:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="oracleImpl" class="com.me.dao.OracleImpl" primary="${oracle.primary}">
</bean>
<bean id="pgsqlImpl" class="com.me.dao.PgsqlImpl" primary="${pgsql.primary}">
</bean>
The values of oracle.primary and pgsql.primary are defined in the file jdbc.properties along with other jdbc (non-boolean) properties.
But it doesn't work and says, "'${oracle.primary}' is not a valid value for 'boolean'"
I have a feeling it is something to do with the xsd validators. Browsing through this site and google gave me this much idea, but got no real solution. Can any body help?
This will not work.
As of 3.2.5.RELEASE only the following bean definition elemets support property placeholder:
parent name
bean class name
factory bean name
factory method name
scope
property values
indexed constructor arguments
generic constructor arguments
See the BeanDefinitionVisitor's visitBeanDefinition method for the details. This method is used by the PlaceholderConfigurerSupport.
I would recommend you to create a feature request in the spring issue management system.
PS: if you create an issue please add a comment to the issues url.

Explicitly inject a bean marked #Component

So for some reason half our project's beans are marked with #Component and injected into other objects marked #Component with the #Autowired annotation, while the other half is wired up explicitly with appliciationContext.xml bean declarations and properties with ref beans values.
Question is, in those xml bean declarations, can I inject one of the objects marked #Component? If so, what will its bean reference name be?
For example;
Some classes are annotated like so;
#Component
public class BeanAImpl{ ... }
Then in applicationContext.xml other classes are wired explicitly as they are not annotated;
<bean id="beanB" class="com.myapp.BeanBImpl"></bean>
Can I inject both 'kinds' of beans in applicationContext.xml?
<bean id="beanUser" class="com.myapp.BeanUserImpl">
<property name="beanA">HOW_TO_GET_REF_TO_BEANA?</property>
<property name="beanB"><ref bean="beanB"/></property>
</bean>
Default name for #Component of type BeanAImpl is beanAImpl, you can use it in XML as you do with manually declared beans (also note that <property> allows shorter syntax):
<property name="beanA" ref = "beanAImpl" />
Alternatively, you can explicitly set a name as #Component("beanA").
Here is what Spring says:
When a component is autodetected as part of the scanning process, its bean name will be generated by the BeanNameGenerator strategy known to that scanner.
If you don't want to rely on the default bean-naming strategy, you may provide a custom bean-naming strategy. First, implement the BeanNameGenerator interface, and be sure to include a default no-arg constructor. Then, provide the fully-qualified class name when configuring the scanner:
<context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerator" />
The deafault implementation is just the class name starting with loer case.
Instead of #Component, I think you want to use #Resource(name="your_id_here"), and then you can <ref> it like you're doing with beanB. Give it a shot.
Source: http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s11.html#beans-resource-annotation

Ability to create bean template in Spring

Lets say I have a situation like this:
<bean id="sample" class="ComlicatedClass" scope="prototype">
<property name="someProperty" value="${propertyValue}"/>
</bean>
I want to be able to create the bean programatically and provide value for propertyValue at runtime (pseudocode ahead):
appContext.getBean("sample", "propertyValue" => "value")
In a way, I want to create "bean template" rather than full defined bean. Is that possible in any way in spring?
EDIT:
The value for propertyValue is known at runtime! There is no way to define it as another bean.
why don't you just do
Sample sample = appContext.getBean("sample");
sample.setSomeProperty(appContext.getBean("someOtherBean"));
Have you looked at the Prototype scope?
The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made. That is, the bean is injected into another bean or you request it through a getBean() method call on the container. As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans.
There is also the #Scope annotation if you are using the Java based container configuration.

Spring: Get inline bean by name

I would like to get inner bean by it's name. Is it possible with Spring API?
Right now I'm using such Spring 2.5 API
ConfigurableApplicationContext.getBean(String paramString)
Example of XML:
<bean id="parent" parent="t_Parent">
<property name="items">
<bean id="child" parent="t_Child">
<property name="ABC" value="test"/>
</bean>
</property>
</bean>
I would like to get inner (t_Child) bean by id "child". E.g. ConfigurableApplicationContext.getBean("child"). Spring can't find such bean (because it's inner). At the same time .getBean("parent") works fine.
Any thoughts?
You can't.
From the docs:
A element inside the or elements is used to define a so-called inner bean. An inner bean definition does not need to have any id or name defined, and it is best not to even specify any id or name value because the id or name value simply will be ignored by the container.
If you need it like that, define it as a regular bean.
You can't, but you can create you inner bean outside (so it's no longer an inner bean...) and then reference it inside the property:
<bean id="child" parent="t_Child">
<property name="ABC" value="test"/>
</bean>
<bean id="parent" parent="t_Parent">
<property name="items" ref="child"/>
</bean>
Apart from the other (mostly valid) answers and solutions, I guess the spring way would be to use the BeanWrapper interface:
final BeanWrapper bw =
new BeanWrapperImpl(applicationContext.getBean("parent"));
Object innerBean = bw.getPropertyValue("child");
But I guess that implies that there must be a getter for the property (not only a setter).
Reference:
BeanWrapper (javadoc, 2.5 version)
Bean manipulation and the BeanWrapper (reference, 2.5 version)
If you move up to Spring 3.x, you should be able to do this with the Spring Expression Language. There are examples of directly referencing a bean property from another property (like in link text). The code to do this from Java would be somewhat similar, although I can't find an exact example of this scenario.
However, I would say that if you're trying to use "getBean()", you're doing something wrong. You could just as easily use the SpEL in your context to define a bean or a bean property that references that inner bean.

Categories