Spring autowiring by type real time example

Upto now we have seen injecting properties into beans manually. In spring there is a feature to Autowire dependencies between beans automatically thats called Autowiring. Autowiring lets you tell spring to try to figure out itself how your beans are all connected. There are several ways of autowiring in spring we gonna start now with type.

Below there is an interface LogWriter.java which is implemented by ConsoleWriter.java and Filewriter.java. Logger.java had dependency on those two classes. so, now we are going to autowire those dependencies by type.

LogWriter.java


package com.soruceforopensource.pojos;

public interface LogWriter {
 public void write(String text);
}

ConsoleWriter.java

package com.sourceforopensource.pojos;

public class ConsoleWriter implements LogWriter {

public void write(String text) {
System.out.println(text);
}

}

FileWriter.java

package com.sourceforopensource.pojos;

public class FileWriter implements LogWriter {

public void write(String text) {
// Write to a file here.
// Dummy implementation
System.out.println("Write to file: " + text);

}

}

Logger.java


package com.sourceforopensource.pojos;

/*
 * Dummy implementation of logger.
 */

public class Logger {

private ConsoleWriter consoleWriter;
 private FileWriter fileWriter;

public void setConsoleWriter(ConsoleWriter writer) {
 this.consoleWriter = writer;
 }

public void setFileWriter(FileWriter fileWriter) {
 this.fileWriter = fileWriter;
 }
 
 public void writeFile(String text) {
 fileWriter.write(text);
 }
 
 public void writeConsole(String text) {
 consoleWriter.write(text);
 }

}

beans.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.xsd">

<bean id="logger" class="com.sourceforopensource.pojo.Logger"
autowire="byType"> //here is autowiring by type

</bean>
<bean id="consolewriter" class="com.sourceforopensource.pojos.ConsoleWriter">
</bean>

<bean id="filewriter" class="com.sourceforopensource.pojos.FileWriter">
</bean>

</beans>

App.java

package com.sourceforopensource.pojos;

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

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("com/sourceforopensource/beans/beans.xml");

Logger logger = (Logger)context.getBean("logger");

logger.writeConsole("Hello there");
logger.writeFile("Hi again");

((ClassPathXmlApplicationContext)context).close();
}

}

output:

Hello there
Write to file: Hi again

Note: There are various pitfalls in wiring stuff automatically, one of those is kind of working on a large project autowiring many thing is hard to understand. If it’s confusing then consider may be not using it. its very good to use under some circumstances. The other problem is ambiguity(that happen if the bean is duplicated in beans.xml then spring feel hard to find which pojo to autowire properties and then it will throw exceptions.

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *