場景
項目正常運行
運行junit測試方式時報以上錯誤。
Failuer Trace:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'permeateController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.kylin.aep.service.PermeateService com.kylin.aep.controller.PermeateController.permeateServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PermeateServiceImpl' defined in file [D:\workspace\aep\target\classes\***\serviceimpl\PermeateServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.kylin.aep.serviceimpl.PermeateServiceImpl]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.**.solr.SolrClient] is defined
分析
根據failure tarce 找到PermeateServiceImpl中有這樣一段代碼:
SolrClient solrClient = SpringContextUtil.getBean(SolrClient.class);
SolrClient.class對應com.**.solr.SolrClient。
可以知道,問題就出現在這一句代碼上。
臨時解決辦法
我要測試的代碼與com.**.solr.SolrClient無關,所以我註釋掉這一行代碼。
然後運行junit測試方法,成功。
追究原因
測試工作完成,現在回過頭研究爲什麼會出現這個問題。
No qualifying bean of type [com.**.solr.SolrClient] is defined
那麼我們來看一下配置文件。
發現SpringServlet.xml中有以下配置:
<!-- solr配置 -->
<bean id="solrClient" class="com.kylin.aep.solr.SolrClient"
scope="singleton">
<constructor-arg name="solrURL" value="${solr.url}" />
<constructor-arg name="coreName" value="${solr.core}" />
<constructor-arg name="maxClient" value="${solr.max.client}" />
</bean>
而SpringServletTest.xml中沒有。
加上以上配置,再運行junit測試方法,果然不報錯。