Spring Setter Dependency Injection Example

By | June 18, 2021

In this post, We will learn about Spring Setter Dependency Injection Example with a Demo Project.

Spring Dependency Injection

  • The dependency Injection is a fundamental aspect of the Spring framework through which the Spring framework container injects objects or instances into other objects or “dependencies”.
  • In another word, We can say that dependency Injection promotes loose coupling of software components and moves the responsibility of managing components onto the Spring container.

Types of Spring Dependency Injection:

  • Setter Based Dependency Injection
  • Constructor Based Dependency Injection 

Setter Based Dependency Injection:

In setter-based DI, the container will call setter methods of the class after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.

Constructor Based Dependency Injection:

The constructor-based dependency injection is accomplished when the Spring container invokes a class constructor with a number of arguments and each representing a dependency on the other class.

When to use setter injection and constructor injection?

The Spring documentation recommends using constructor-based injection for mandatory dependencies, and setter-based injection for optional Dependency

Now Let’s try to understand Setter Based dependency injection(DI) using the below demo Project.

pom.xml

Employee.java

Here You can Observe that the Employee depends on the Address object and this dependency Spring Container injects at run time by calling the setAddress(Address address) method.

Address.java

applicationContext.xml

In this config file <property name=”address” ref=”address”></property> represents setter based DI

ClientTest.java

If you run ClientTest.java as Java Application then it will give the below output: 

Employee [employeeId=102883, employeeName=Sean, salary=80000.0]
Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049]

That’s all about Spring Setter Dependency Injection Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.