问题描述
我有一个包裹连接池类,类会从Spring配置其连接的详细信息,如下图所示:
<豆的id =jedisConnector级=com.legolas.jedis.JedisConnector初始化方法=的init 灭法=消灭>
<属性名=主机值=$ {} jedis.host/>
<属性名=端口值=$ {} jedis.port/>
< /豆>
这个bean是在服务以后使用,并与自动装配的 @Autowire
注释。
我的问题是,我怎么能在服务复制这个bean,并给它不同的连接详细信息,然后 @Autowire
它。
这意味着除了上面我将有:
<豆的id =jedisConnectorPOD级=com.legolas.jedis.JedisConnector初始化方法=的init 灭法=消灭>
<属性名=主机值=$ {} jedis.pod.host/>
<属性名=端口值=$ {} jedis.pod.port/>
< /豆>
和在服务:
@Autowired // ID jedisConnector豆
JedisConnector beanA;ID jedisConnectorPOD的@Autowired //豆
JedisConnector beanB;
您可以将 @Autowired
与 @Qualifier
,但在这种情况下,而不是 @Autowired
,我建议使用 @Resource
:
@资源(名称=jedisConnector)
JedisConnector beanA;@Resource(NAME =jedisConnectorPOD)
JedisConnector beanB;
或者更简单的:
@Resource
JedisConnector jedisConnector;@Resource
JedisConnector jedisConnectorPOD;
I have a class which wraps a connection pool, the class gets its connection details from a spring configuration as shown below:
<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>
This bean is later used in a service and is autowired with the @Autowire
annotation.
My question is, how can i duplicate this bean and give it different connection details and then @Autowire
it in the service.meaning In addition to above I will have :
<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>
and in the service:
@Autowired //bean of id jedisConnector
JedisConnector beanA;
@Autowired //bean of id jedisConnectorPOD
JedisConnector beanB;
You can combine @Autowired
with @Qualifier
, but in this case instead of @Autowired
, I suggest using @Resource
:
@Resource(name="jedisConnector")
JedisConnector beanA;
@Resource(name="jedisConnectorPOD")
JedisConnector beanB;
or even simpler:
@Resource
JedisConnector jedisConnector;
@Resource
JedisConnector jedisConnectorPOD;
这篇关于自动装配的相同类的两个不同的豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!