Issue
I am digging into Springs framework recently and just came up with a small question. Suppose if we have a class like this:
class A
{
@Autowired
private B hello;
....
....
}
and in the xml file I have defined like this:
<bean id="abc" class="org.eclipse.packageName.B"/>
Should the name of id in the bean created and the reference name mentioned in the class A be same or not?
And another question is: how does the compiler knows that the bean of this class is mentioned in the xml if we @autowired a field.How is this linking done internally?
Solution
No, your bean id doesn't need to be the same as the field name in your referencing class. Injection is based on type and not on id in Spring (unless you use @Qualifier
annotation).
The injection is provided by Spring IoC container at runtime.
Answered By - davioooh