Search This Blog

Featured

Wednesday, August 14, 2013

Resolve @Autowire from many candidates.

How to resolve @Autowire property from multiple instances?

or you got 'Spring finding multiple bean definition error' ?
Its simple.!

You need to use @Qualifier annotation here...!

public class MovieRecommender {
     @Autowired
    @Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}

and your bean application context configuration will have this.

<context:annotation-config/>
<bean class="example.SimpleMovieCatalog">
<qualifier value="main"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
<!-- inject any dependencies required by this bean -->
</bean>

Now you can switch between two qualifiers when ever you want too..! good luck.


No comments: