Tuesday, 26 June 2018

What is Spring dependency injection by setter method? | Setter dependency injection in spring - Playlist

🚀 Want to Master Spring & Java Development?

Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!

🔔 Subscribe to YouTube Channel

Spring Setter Injection: Injecting Primitives & Strings

In the Spring Framework, Dependency Injection (DI) is the core mechanism used to achieve Inversion of Control (IoC). One of the most widely used forms of DI is Setter Injection, where the Spring IoC container injects property values into your bean after calling the default no-argument constructor.

1. What is Setter Injection?

Setter injection occurs when dependencies or simple property values are provided through public setter methods (e.g., setName(String name), setAge(int age)).

  • Spring first calls the no-arg constructor to instantiate the bean.
  • It then identifies the <property> sub-elements defined in your configuration.
  • Finally, Spring invokes the corresponding setter methods to populate the bean's properties.

2. Injecting Primitives & String Literals

When injecting basic data types like int, float, boolean, or String, you use the value attribute inside the <property> tag:

  • Automatic Type Conversion: Spring’s PropertyEditor mechanism automatically converts string representations in the XML file into the target primitive or wrapper type (such as int, double, or boolean).
  • Literal Values: Strings and primitives are treated as literal values, requiring no bean reference (ref) attributes.

3. Step-by-Step Configuration Example

Consider an Employee bean with fields like id (int) and name (String). Here is how you wire it in your Spring XML configuration:

<bean id="employee" class="com.ram.Employee">
  <property name="id" value="101" />
  <property name="name" value="Peter" />
</bean>

4. Key Benefits of Setter Injection

Setter injection offers several distinct advantages in application development:

  • Flexibility: Dependencies are optional or can be reconfigured later during execution.
  • Readability: Explicit property names in configuration files make it clear which values correspond to which bean fields.
  • Circular Dependencies: Handles circular dependencies more gracefully than strict constructor injection.

📺 Explore More Spring Framework Tutorials

Continue expanding your Java and Spring knowledge with these related tutorials from our channel:


Click here to watch on Youtube:
https://www.youtube.com/watch?v=X0zj-i3EY8k&list=PLmCsXDGbJHdh1ScvY-jwDDEVFRslPMhkn 

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • 2 comments:

    1. Its Very Use Full Information , Keep Posting Blogs Of Angular , For More Information Search Angular Online Training , Thanking You

      ReplyDelete

    Tutorials