Example of CustomAutowireConfigurer in Spring
April 27, 2013
On this page we will provide example of CustomAutowireConfigurer in spring. CustomAutowireConfigurer is the implementation of BeanFactoryPostProcessor. CustomAutowireConfigurer is used to create custom qualifier. Qualifier is used to resolve conflicts of dependency injection where more than one beans are eligible for autowiring. We can create custom qualifier in two ways.
1. Using @Qualifier Annotation (find the link)
2. Using CustomAutowireConfigurer
Here on this page, we will provide a complete example of CustomAutowireConfigurer.
Create Custom Qualifier Annotation
We are creating two custom qualifiers without any method.DeerQualifier.java
package com.concretepage.qualifier; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface DeerQualifier { }
package com.concretepage.qualifier; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface FoxQualifier { }
Configure CustomAutowireConfigurer in XML
Find the XML file to configure CustomAutowireConfigurer and we will also configure qualifier with our beans.app-conf.xml
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer"> <property name="customQualifierTypes"> <set> <value>com.concretepage.qualifier.FoxQualifier</value> <value>com.concretepage.qualifier.DeerQualifier</value> </set> </property> </bean> <bean class="com.concretepage.bean.AnimalService"/> <bean class="com.concretepage.bean.Fox"> <qualifier type="FoxQualifier"/> </bean> <bean class="com.concretepage.bean.Deer"> <qualifier type="DeerQualifier"/> </bean> </beans>
Create Beans
Find the classes which will configured as bean in XML.Animal.java
package com.concretepage.bean; public interface Animal { void printName(); }
package com.concretepage.bean; public class Deer implements Animal { @Override public void printName() { System.out.println("--- Deer ---"); } }
package com.concretepage.bean; public class Fox implements Animal { @Override public void printName() { System.out.println("--- Fox ---"); } }
package com.concretepage.bean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.concretepage.qualifier.DeerQualifier; @Service public class AnimalService { @Autowired @DeerQualifier private Animal animal; public Animal getAnimal() { return animal; } }
Main Class to Test Application
Find the main class to test the application.SpringDemo.java
package com.concretepage; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.concretepage.bean.AnimalService; public class SpringDemo { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml"); AnimalService service = context.getBean(AnimalService.class); service.getAnimal().printName(); context.close(); } }
11:35:59.934 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'com.concretepage.bean.AnimalService#0' --- Deer --- 11:35:59.934 [main] INFO o.s.c.s.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6a38e57f: startup date [Thu Jan 14 11:35:59 IST 2016]; root of context hierarchy