Spring 3: Type safe dependency injection

Before jumping to Type Safe Dependency Injection from Spring, I would like to to discuss the way we have been doing it earlier. We have been using dependency injection by type with the help of Autowired annotation from Spring.
Something like this would inject the spring bean.

@Autowired
private StudentDao studentDao; // Autowires by type. Injects the instance whose type is StudentDao

But if we have more than one spring bean of one type then we use Qualifier Annotation along with Autowired which in facts injects the spring bean by name.

An application context with :

<bean id="studentDao1" class="StudentDao" />
<bean id="studentDao2" class="StudentDao" />

So now if we have two instances of StudentDao (studentDao1 and studentDao2), we can inject spring bean by name.

@Autowired
@Qualifier("studentDao1")
private StudentDao studentDao1;

@Autowired
@Qualifier("studentDao2")
private StudentDao studentDao2;

Same thing can be achieved with Resource annotaion specified by JSR-250. So we can inject a bean into a field or single parameter method with this annotation. Autowired is little more flexible than Resource since it can be used with multi parameter method as well as constructors.
We can inject bean by name with Resource annotation in the following manner.

@Resource
private StudentDao studentDao1;

Type safe dependency injection in Spring 3

Define a custom annotation using @Qualifier

To identify the injected bean without specifying the name, we need to create a custom annotation. This is an equivalent procedure to the use of JSR 330 annotations(Inject) in CDI.

@Target({ElementType.Field, ElementType.Parameter})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @Interface Student  {
}

Now assign this custom annotation to implementation of EntityDao Interface

@Component
@Student
public class StudentDao implements EntityDao   {
}

@Component tells Spring that this a bean definition. @Student annotation is used by Spring IoC to identify StudentDao as EntityDao’s implementation whenever reference of EntityDao is used.
Inject the bean using @Autowired and custom qualifier
Something like this.

@Autowired
@Student
private EntityDao studentDao; // So the spring injects the instance of StudentDao here.

This makes less use of String-names, which can be misspelled and are harder to maintain.

Reference: How to use Type safe dependency injection in Spring 3? from our JCG partner Saurab Parakh at the Coding is Cool blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Spring 3: Type safe dependency injection"

  1. Erek says:

    Thanks a lot. Exactly what I was searching for.

Leave a Reply


− 6 = zero



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books