Tutorials

Spring Autowiring


Autowiring byType


Autowiring a bean byType simply checks the data type of the property of the mapped class and then reads the configuration metadata (i.e configuration xml or java based configuration) to find the bean with matching data type. If found then that bean is automatically wired. This is why it is known as autowiring byType as it works for matching data type between java pojo and configuration file.

 

Explained below is an example of autowiring bean byType.

 

 

 

Java POJO - WriteMessage.java

package com.autowirepkg;

public class WriteMessage {

	private String message;
	private String sender;
	private String reciever;
	
	public WriteMessage(String message,String sender,String reciever ){
		this.message = message;
		this.sender = sender;
		this.reciever = reciever;
	}
	
	public void messageDetails(){
		System.out.println("Message: "+ message);
		System.out.println("Sender: "+ sender);
		System.out.println("Reciever: "+ reciever);
	}
}

Java POJO - ReadMessage.java

This class has property named 'writeMsg' whose data type is WriteMessage.

package com.autowirepkg;

public class ReadMessage {

	WriteMessage writeMsg; //WriteMessage type will be autowired in XML

	public WriteMessage getWriteMsg() {
		return writeMsg;
	}

	public void setWriteMsg(WriteMessage writeMsg) {
		this.writeMsg = writeMsg;
	}
	
	public void readMsg(){
		writeMsg.messageDetails();
	}
}

XML Configuration - appContextAutowire.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="writeMsg" class="com.autowirepkg.WriteMessage">
<constructor-arg index="0" value="You are excellent learner"></constructor-arg>
<constructor-arg index="1" value="tutorialsinhand"></constructor-arg>
<constructor-arg index="2" value="Readers"></constructor-arg>
</bean>

<bean id="ReadMsgBean" class="com.autowirepkg.ReadMessage" autowire="byType">
</bean>

</beans>

Test Class - TestSpringClass.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.autowirepkg.ReadMessage;

public class TestSpringClass {

	public static void main(String[] args) {		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("appContextAutowire.xml");
		
		ReadMessage readMessage = (ReadMessage) context.getBean("ReadMsgBean");
		readMessage.readMsg();
	}

}

OUTPUT

Message: You are excellent learner
Sender: tutorialsinhand
Reciever: Readers
 


Please Share this page
Views : 43
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!