Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

5. Using dependency injection with annotations

As of Spring 2.5 it is possible to configure the dependency injection via annotations. I recommend to use this way of configuring your Spring beans. The next chapter will also describe the way to configure this via XML.

Create a new Java project "de.vogella.spring.di.annotations.first" and include the minimal required spring jars into your classpath.

Copy your model class from the de.vogella.spring.di.model project into this project.

You need now to add annotations to your model to tell Spring which beans should be managed by Spring and how they should be connected.

Add the @Service annotation the MySpringBeanWithDependency.java and NiceWriter.java. Also define with @Autowired on the setWriter method that the property "writer" will be autowired by Spring.

Tip

@Autowired will tell Spring to search for a Spring bean which implements the required interface and place it automatically into the setter.

			
package testbean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import writer.IWriter;

@Service
public class MySpringBeanWithDependency {
	private IWriter writer;

	@Autowired
	public void setWriter(IWriter writer) {
		this.writer = writer;
	}

	public void run() {
		String s = "This is my test";
		writer.writer(s);
	}
}

		

			
package writer;

import org.springframework.stereotype.Service;

@Service
public class NiceWriter implements IWriter {
	public void writer(String s) {
		System.out.println("The string is " + s);
	}
}

		

Under the src folder create a folder META-INF and create the following file in this folder. This is the Spring configuration file.

			
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="testbean" />
	<context:component-scan base-package="writer" />

</beans>
		

Tip

You can also configure the log4j logger (this is optional) by copying the following file into the source folder.

					
log4j.rootLogger=FATAL, first
log4j.appender.first=org.apache.log4j.ConsoleAppender
log4j.appender.first.layout=org.apache.log4j.PatternLayout
log4j.appender.first.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

				

Afer this setup you can wire the application together. Create a main class which reads the configuration file and starts the application.

			
package main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import testbean.MySpringBeanWithDependency;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"META-INF/beans.xml");
		BeanFactory factory = context;
		MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory
				.getBean("mySpringBeanWithDependency");
		test.run();
	}
}

		

If you run the application then the class for the IWriterInterface will be inserted into the Test class. By applying the dependency injecting I can later replace this writer with a more sophisticated writer.

As a result the class Test does not depend on the concrete Writer class, is extensible and can be easily tested.