![](/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.