Spring Autowiring byName & byType Example

By | June 22, 2021

In this post, We will learn about the Spring Autowiring byName & byType Example using a Demo Project

Autowiring in Spring

The autowiring in the spring framework enables us to inject the object dependency implicitly. It’s internally using setter or constructor injection.

One thing You should keep in mind that auto wiring can’t be used to inject primitive and string data types values. It works with Custom Object/reference only.

Advantage of Autowiring

When we use auto wiring then we require less code because we don’t need to write the code to inject the dependency in the config file explicitly.

Disadvantage of Autowiring

The downside is that the programmer has no control over auto wiring. and auto wiring can’t be used for primitive and string data types.

Autowiring modes

There are many auto wiring modes:

No. Mode Description
1 no It is the default auto wiring mode. It means no auto wiring by default.
2 byName The byName auto wiring mode injects the bean dependency according to the name of the bean. In such a case, the property name and Spring bean name must be the same. It internally calls the setter method to inject dependency at runtime.
3 byType The byType mode injects the bean dependency according to bean type. Here property name and bean name may be different. It internally calls the setter method to inject dependency at runtime.
4 constructor The constructor auto wiring mode injects the bean dependency by calling the constructor of a bean class. It usually calls the constructor to inject dependency at runtime.
5 autodetect This auto wiring mode is deprecated since Spring 3.

pom.xml

Employee.java

Address.java

applicationContext.xml

We need to use autowire attribute of the bean element to apply the autowire modes as below.

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=address line 1, addressLine2=address line 2, city=Delhi, zipCode=959049]

That’s all about Spring Autowiring byName & byType 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 @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection 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.