Video summary

#10 Constructor and Setter Injection in Spring

Main summary

Key takeaways

Educational

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.
  • 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.
  • 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)

  1. Create an XML configuration file such as spring.xml.
  2. Define beans using <bean> tags, following an example pattern like:
    • A bean named Dev of class Dev
    • A bean named Dev one of class dev/Dev (differentiated bean id)
    • A bean named laptop of class laptop
  3. 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 as age (an int).
  • Ensure accessors exist:
    • Create a getter and setter for the field (getAge() / setAge(...)).
  • 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)
  • Verify behavior:
    • If Dev one does not define the age property, age remains default (0 for int).
    • If you remove/restore the property, the printed value changes accordingly.

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 internal age field.
  • 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 the age parameter)
  • 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), or
      • index="0", index="1" to map values in order.

4) Injecting a reference type (e.g., laptop) into another bean

Problem demonstrated

  • If Dev contains a laptop reference 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> for Dev:
    • Add:
      • <property name="laptop" ref="labOne">
    • Use ref, not value.
  • The ref must point to the bean id of the laptop bean in the container.
  • Example adjustment shown:
    • Rename the laptop bean id (e.g., from laptop to lap one / lab one) so the reference matches.
  • 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”.

Constructor injection for a reference

  • Create a constructor in Dev that accepts Laptop (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.

Original video