Spring autowiring by type

In Previous article we have learned that Spring support different type of autowiring. One of them is byType.

Autowiring byType means whenever spring finds any property to be autowired, it will search for exactly one bean of given property type 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 byType can not be used here.

How to Enable byType Autowiring

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

Here company bean has Address as property.

Spring Autowiring byType Example

Here we are using two beans Address and Company like below.





Spring Bean Metadata

Here we are using XML to provide the metadata of Beans. Below is the content of XML.


Spring Autowiring

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



Output would be something like below


If you uncomment other Address bean(addressDuplicate) in XML file you will get below exception.


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


Leave A Comment

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