![](/img/trans.png)
[英]Unable to get the spring context.getBean() from <util:properties>
[英]which context.getBean to use when get the bean from spring
在以下链接中http://docs.spring.io/spring-amqp/reference/html/quick-tour.html
它在XML中定义了一个bean
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>
然后在调用代码中,它使用了
AmqpTemplate template = context.getBean(AmqpTemplate.class);
为什么使用
context.getBean(AmqpTemplate.class);
代替
context.getBean("amqpTemplate");
getBean(AmqpTemplate.class)是什么意思? 我找不到它由xml定义。
如果使用context.getBean("amqpTemplate")
,则需要将结果强制转换为AmqpTemplate,而getBean(AmqpTemplate.class)
会自动将结果转换为AmqpTemplate。 请注意,只有在上下文中只有一个具有AmqpTemplate类的bean时, getBean(AmqpTemplate.class)
才能工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.