簡體   English   中英

如何在Beanfactory中使用@Autowired

[英]How to use @Autowired with Beanfactory

我已經使用實例化了bean容器

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("ApplicationContext.xml"));

而且我還使用以下方法注冊了AutowiredAnnotationBeanPostProcessor

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

但是當我這樣做時

 @Autowired
 @Qualifier("emailService")
 private MessageService messageService;

它給出的NPE表示自動裝配不適用於注釋,這是什么問題? 我不能將BeanFactory與@Autowired一起使用嗎?

根據您的問題,可能有兩種解決方案。

1)首先,您需要檢查您的類是否使用@ Component,@ Service進行注釋

2)並且您不能使用新的Object調用類,您只需要從applicationcontext中提取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM