In this article lets take a look into Autowiring byType, This type of Autowiring in Spring the Spring framework injects dependency based on type(class). Unlike Autowiring byName bean id and property name can be different. It tries to match the property type to the bean type in the configuration file, If a bean is found then Spring internally uses Setter Injection and the object will be injected. If not found then it will throw exception.
Folder Structure:
- Create a new Java Project “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
- Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.
commons-logging-1.1.1.jar
spring-beans-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar - Create the Java classes Bean1.java, Bean2.java and ClientLogic.java under com.javainterviewpoint folder.
- Place our configuration file SpringConfig.xml in the src directory
Bean1.java
Bean1 class will have Bean2 class as a property(b2) and will have its corresponding getters and setters. It also has show() method which in-turn calls the disp() method of the Bean2 class. Value for b2 will be set through the configuration file using autowiring
package com.javainterviewpoint;
public class Bean1
{
public Bean2 b2;
public Bean2 getB2() {
return b2;
}
public void setB2(Bean2 b2) {
this.b2 = b2;
}
public void show()
{
System.out.println("Bean1 show() method called");
b2.disp();
}
}
Bean2.java
Bean2 class has nothing but a single disp() method which will be called from Bean1 class.
package com.javainterviewpoint; public class Bean2 { public void disp() { System.out.println("Bean2 disp() method called"); } }
SpringConfig.xml
In our configuration file we have defined separate id for each bean Bean1 and Bean2 classes
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="b1" class="com.javainterviewpoint.Bean1" autowire="byType"></bean> <bean id="xyz" class="com.javainterviewpoint.Bean2"></bean> </beans>
- We mentioned autowire to byType while declaring Bean1, it actually searches for the property type in Bean1 class and matching type in the configuration file if found then the object(Bean2) will be injected. Here we have mentioned bean id for Bean2 class as “xyz” as property name and bean id need not be same.
<bean id="b1" class="com.javainterviewpoint.Bean1" autowire="byType"></bean> <bean id="xyz" class="com.javainterviewpoint.Bean2"></bean>
ClientLogic.java
package com.javainterviewpoint; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the Bean1 instance Bean1 bean1 = (Bean1)bf.getBean("b1"); //Lets call the show() method bean1.show(); } }
- Resource class reads our Configuration File(SpringConfig.xml)
- BeanFactory class read all the bean definition mentioned in the config file.
- Get the Bean1 Class instance by calling the getBean() method over the bean factory.
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
- Now call the show() method of the Bean1 class, which inturn calls the disp() method of Bean2 class. As Bean2 object will be injected automatically using autowiring byType.
Output
Once we run our ClientLogic.java we will get the below output
Bean1 show() method called Bean2 disp() method called
Leave a Reply