Spring exclude beans from autowiring example

The Spring exclude beans from autowiring example show you do the xml configuration to exclude beans from autowiring. In the configuration file, we add a autowire-candidate attribute which is set to false. This means that the container will excludes that specific bean from autowiring.

Let’s begin:

Create Autowiring Beans
We create Fruit bean and autowire bean using @Autowired annotation, for example autowiring setter injection.

Next, we create Country bean which is autowired into the Fruit bean.

Exclude bean from Autowiring
As you know, Two Fruit and Country beans will be autowired automatically by Spring container. However, in case you want to exclude a bean from autowiring. you can use the autowire-candidate attribute of the element. This makes container understand that the bean is not longer autowiring candidate.

Demo Spring Application
In this Spring exclude beans from autowiring example, we create main class to test what we say above

Run the main above, the console will print the exception message:

That’s all on the Spring exclude beans from autowiring example.

References
Autowire-Candidate Doc

Download complete source code, click link below

spring-exclude-bean-from-autowiring-example.zip (23 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons