Spring @Autowired Annotation With Setter Injection Example

By | June 22, 2021

In this post, We will learn about the Spring @Autowired Annotation With Setter Injection Example using 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:

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

@Autowired annotation

Spring @Autowired annotation is mainly used for automatic dependency injection. This annotation may be applied to before class variables and methods for auto wiring byType. We can also use @Autowired annotation on the constructor for constructor-based spring auto wiring.

Now Let’s try to understand Setter Based dependency injection(DI) using @Autowired Annotation With the help of the below demo Project.

pom.xml

Employee.java

Address.java

applicationContext.xml

To enable @Autowired annotation in Spring Framework we have to use <context:annotation-config/> tag in the config file as below.

ClientTest.java

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

That’s all about Spring @Autowired Annotation With Setter 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 Setter Dependency 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.