Video summary
#10 Constructor and Setter Injection in Spring
Main summary
Key takeaways
Main ideas / lessons
- Goal: Explain how Spring (without Spring Boot) creates objects using an IoC container and how to use dependency injection to wire one object into another.
- Object creation in Spring Framework (XML config):
- Use an XML file (e.g.,
spring.xml) with multiple<bean>definitions. - When the app runs, Spring reads these bean definitions and instantiates the objects inside the Spring container.
- Use an XML file (e.g.,
- Why injection is needed:
- If a class only has a reference field (e.g.,
laptop), and Spring hasn’t injected it, the field remains null, leading to a NullPointerException when methods are called on it.
- If a class only has a reference field (e.g.,
- Two types of injection taught:
- Setter Injection (set values after object creation)
- Constructor Injection (provide values during object creation)
Detailed methodology / instruction list (as presented)
1) Configure bean creation (no Spring Boot)
- Create an XML configuration file such as
spring.xml. - Define beans using
<bean>tags, following an example pattern like:- A bean named
Devof classDev - A bean named
Dev oneof classdev/Dev(differentiated bean id) - A bean named
laptopof classlaptop
- A bean named
- Result:
- On startup, Spring creates these objects and stores them in the IoC / Spring container.
- The container holds distinct instances per bean id (e.g.,
Dev,Dev one,laptop).
2) Setter Injection for a primitive value (e.g., age)
- In the target class (e.g.,
Dev), include a field such asage(anint). - Ensure accessors exist:
- Create a getter and setter for the field (
getAge()/setAge(...)).
- Create a getter and setter for the field (
- In
spring.xml, inside the relevant<bean>definition for the correct dev bean (e.g.,Dev):- Add a
<property>entry, specifying:name="age"(the field to set)value="12"(the value Spring should inject)
- Add a
- Verify behavior:
- If
Dev onedoes not define theageproperty,ageremains default (0 forint). - If you remove/restore the property, the printed value changes accordingly.
- If
3) Constructor Injection for a primitive value (e.g., age)
- Modify the class to include a parameterized constructor, e.g.:
Dev(int age)that sets the internalagefield.
- In
spring.xml, switch injection style for the relevant bean (e.g.,Dev one):- Use
<constructor-arg>instead of<property>. - Provide:
value="14"(the constructor argument for theageparameter)
- Use
- Notes given:
- If there is only one constructor parameter, you can omit specifying
name. - If there are multiple parameters, you may specify:
name="..."(by parameter name), orindex="0",index="1"to map values in order.
- If there is only one constructor parameter, you can omit specifying
4) Injecting a reference type (e.g., laptop) into another bean
Problem demonstrated
- If
Devcontains alaptopreference field but it isn’t injected, it stays null. - Calling
laptop.compile()then triggers a NullPointerException.
Setter injection for a reference
- In
Dev, ensure you have getters/setters for the reference field (e.g.,setLaptop(...)). - In
spring.xml, inside the target<bean>forDev:- Add:
<property name="laptop" ref="labOne">
- Use
ref, notvalue.
- Add:
- The
refmust point to the bean id of the laptop bean in the container. - Example adjustment shown:
- Rename the laptop bean id (e.g., from
laptoptolap one / lab one) so the reference matches.
- Rename the laptop bean id (e.g., from
- Additional requirement mentioned:
- When using setter injection for
laptop, Spring expects the field to be writable (has a setter). - If not, Spring can throw an error such as “not writable / invalid”.
- When using setter injection for
Constructor injection for a reference
- Create a constructor in
Devthat acceptsLaptop(or the laptop type), e.g.:Dev(Laptop laptop)
- In
spring.xml, use:<constructor-arg ref="labOne" />for the constructor argument.
Key concept comparison (stated)
- Use Constructor injection when the dependency is compulsory (must exist at object creation time).
- Use Setter injection when the dependency is optional (can be set later after the object is created).
Speakers / sources
- Speaker: “welcome back aliens” (the video presenter / narrator; no name given in subtitles)
- Source: Spring Framework concepts demonstrated via the video’s own example classes/configuration (e.g.,
Dev,laptop,spring.xml)—no external named sources referenced.