Page 2 of 2
Injection in Spring
- How many ways can one inject source bean into target by using Spring ?
There are two ways how you can inject source bean into the target bean. Constructor injection and setter inject.
- What is the difference between Constructor injection and setter injection ?
Constructor injection Setter Injection
- Happens on the constructor of the bean Happens in the setter method / mutator method in the bean
- <constructor-arg> tag is used in the xml definition <property> tag is used
- Use this for all mandatory fields Use this for all optional fields
- Less verbose , but looks bad if you have a lot of More verbose, but gives access to the fields even after the bean is constructed.
composed objects, for examle if your constructor
takes a lot of argument this can be a bad idea for
- Can Spring inject existing Java classes into a target bean ?
Yes, Spring can inject existing Java classes into the target bean, for example you use <property name="pincode" value="343222" /> , here the value 343222 is a string, you can mention the type of the bean by mentioning type="java.lang.String" as well.
- Does the order in which the declaration of bean matter ?
- What is lazy loading of beans?
- What is p namespace ?
- What is automatic name-by-class behavior ?
- What is top level and inner anonymous bean ?
- Can a Bean have more than one name ?