Spring autowiring by name example

In Previous article we have learned about autowiring byType. In this article we will learn about the Autowiring by Name.

Autowiring byName means whenever spring finds any property to be autowired, it will search for exactly one bean of given property name in container. If Spring find one(unique bean) it will autowire it. If it doesn’t find any, no auto wiring will be done(Property will not be set). If there are more than one bean of same type in container then Spring will throw Exception that byName can not be used here.

How to Enable byType Autowiring

byName Autowiring can be enabled by using autowire=”byName” like below.

 

Here company bean has Address as property.

Spring Autowiring byName Example

In this example we will use two beans named Address and Company.

Address.java

 

Company.java

Spring Bean Metadata

JBTBean.xml

Spring Autowiring

If you try to get the Company Bean by using below line.

Output would be something like below

If you want to get source code of given article please send me a mail. With Subject line as “Spring autowiring by Name

1 Comment Spring autowiring by name example

Leave A Comment

Your email address will not be published. Required fields are marked *