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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javabycode.spring.core.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Fruit { private String name; private Country country; public void setName(String name) { this.name = name; } @Autowired public void setCountry(Country country) { this.country = country; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Next, we create Country bean which is autowired into the Fruit bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode.spring.core.autowired; public class Country { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Country{" + "name='" + name + '\'' + '}'; } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean class="com.memorynotfound.spring.core.autowired.Job" autowire-candidate="false"> <property name="name" value="Java Developer"/> </bean> <bean class="com.memorynotfound.spring.core.autowired.Person"> <property name="name" value="John Doe"/> </bean> </beans> |
Demo Spring Application
In this Spring exclude beans from autowiring example, we create main class to test what we say above
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring.core.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Fruit fruit = context.getBean(Fruit.class); System.out.println(fruit); } } |
Run the main above, the console will print the exception message:
1 |
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.javabycode.spring.core.autowired.Country] found for dependency [com.javabycode.spring.core.autowired.Country]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} |
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 (319 downloads)