This article is stem from the question on stackoverflow. When I read about this question, immediately, I think that it is very useful for me in reality.
Problem
Assuming that you have two configuration in xml file about connections.
<bean id="jedisConnector" class="com.legolas.jedis.JedisConnector" init-method="init" destroy-method="destroy">
<property name="host" value="${jedis.host}" />
<property name="port" value="${jedis.port}" />
</bean>
<bean id="jedisConnectorPOD" class="com.legolas.jedis.JedisConnector" init-method="init" destroy-method="destroy">
<property name="host" value="${jedis.pod.host}" />
<property name="port" value="${jedis.pod.port}" />
</bean>
Now, we want to create two object that achieve all informations about two connections with id are jedisConnector and jedisConnectorPOD by using annotations.
Normally, we will think about @Autowired
annotation with the below example:
@Autowired //bean of id jedisConnector
JedisConnector beanA;
@Autowired //bean of id jedisConnectorPOD
JedisConnector beanB;
But, it does not work like our thought. So how do you use annotations to make it works?
Solution
To solve this problem, we two three solutions.
-
Use
@Resource
@Resource(name="jedisConnector") JedisConnector beanA; @Resource(name="jedisConnectorPOD") JedisConnector beanB;
or
@Resource JedisConnector jedisConnector; @Resource JedisConnector jedisConnectorPOD;
-
Use
@Autowired
with@Qualifier
annotation@Autowired @Qualifier("jedisConnector") JedisConnector beanA; @Autowired @Qualifier("jedisConnectorPOD") JedisConnector beanB;
Thanks for your reading.