我有一個彈簧控制器與自動裝配的對象,春天說,儘管我看到 - 儘管我看到 - 在日誌文件中找不到 - 爲它創建的bean /對象在根應用程序上下文中。它在部署應用程序期間發生(在Tomcat中)。春天web應用程序 - autowire - 找不到bean注入
我試圖將@Qualifier添加到@Autowired字段,但它沒有解決問題。
控制器:
package com.maha.testspring.endpoints.webrest.controllers;
import com.maha.testspring.services.TestSpringService;
@Controller
@RequestMapping("/testspring")
public class TestSpringController
{
@Autowired
@Qualifier("testSpringService")
private TestSpringService testSpringService;
...
}
戰爭testspring的端點,webrest-1.0-SNAPSHOT/WEB-INF/web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
戰爭testspring的端點,webrest-1.0 -SNAPSHOT/WEB-INF/spring/servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
...
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="com.maha.testspring.endpoints.webrest.controllers" />
</beans:beans>
戰爭testspring的端點,webrest-1.0-SNAPSHOT/WEB-INF /春/根的context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
<bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>/applicationContext.testspring.services.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
服務實現類:(罐中,testspring - 服務 - IMPL-1.0-SNAPSHOT.jar - 在戰爭的testspring的端點,webrest-1.0-SNAPSHOT \ WEB-INF \ lib文件夾)
package com.maha.testspring.services;
import org.springframework.stereotype.Service;
@Service("testSpringService")
public class TestSpringServiceImpl implements TestSpringService {
public void testIt() { System.out.println("..."); }
}
服務接口:(罐中,testspring服務S-接口-1.0-SNAPSHOT.jar - 在戰爭的testspring的端點,webrest-1.0-SNAPSHOT \ WEB-INF \ lib文件夾)
package com.maha.testspring.services;
public interface TestSpringService
{
public void testIt();
}
applicationContext.testspring.services.xml(在罐子, testspring服務,實現了一套-1.0-SNAPSHOT.jar - 在戰爭的testspring的端點,webrest-1.0-SNAPSHOT \ WEB-INF \ lib文件夾)
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<context:spring-configured/>
<context:annotation-config />
<context:component-scan base-package="com.maha.testspring.services"/>
</beans>
記錄 - 顯示TestSpringServiceImpl是爲@Service處理(作爲稍後注入依賴關係時的候選組件)
annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/C:/osd/Tomcat%208.0/webapps/testspringwebrest/WEB-INF/lib/testspring-services-impl-1.0-SNAPSHOT.jar!/com/maha/testspring/services/TestSpringServiceImpl.class]
support.ClassPathXmlApplicationContext - Bean factory for org[email protected]41e89deb: org.s[email protected]7487b2bc
記錄顯示創建bean實例的該類:當試圖創建控制器
support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testSpringService'
support.DefaultListableBeanFactory - Creating instance of bean 'testSpringService'
support.DefaultListableBeanFactory - Eagerly caching bean 'testSpringService' to allow for resolving potential circular references
support.DefaultListableBeanFactory - Finished creating instance of bean 'testSpringService'
錯誤 - 找不到豆注入:
support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testSpringController'
support.DefaultListableBeanFactory - Creating instance of bean 'testSpringController'
annotation.InjectionMetadata - Registered injected element on
class [com.maha.testspring.endpoints.webrest.controllers.TestSpringController]: AutowiredFieldElement for
private com.maha.testspring.services.TestSpringService com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService
annotation.InjectionMetadata - Processing injected element of bean 'testSpringController':
AutowiredFieldElement for private
com.maha.testspring.services.TestSpringService
com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService
support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSpringController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.maha.testspring.services.TestSpringService com.maha.testspring.endpoints.webrest.controllers.TestSpringController.testSpringService;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type
[com.maha.testspring.services.TestSpringService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
你可以在root-context.xml中放一行:''並重試? –
user2953113
我做了,並工作 - 謝謝(我刪除了applicationContext.testspring.services.xml,並在根上下文中,XML,我刪除了ClassPathXmlApplicationContext,並添加了只有你有的行)。我試圖看看使用ClassPathXmlApplicationContext的問題是什麼。我要清理一下這個問題。我在日誌文件中發現它不起作用時,它看起來類似於:spring說'完成創建bean'testSpringService'的實例。但是,它無法在上下文中找到它。注意 - 在清理上述內容之前,日誌文件看起來不正確 - 它說testSpringServiceImpl的名稱爲 – Meta
注意:我將它運行在獨立的Java應用程序中 - 根應用程序上下文和Web應用程序不會成爲問題 - 並且它也可以工作。我循環了從context.getBeanDefinitionNames()返回的所有名稱並將'testSpringService'作爲名稱。以下工作: \t'ApplicationContext上下文=新ClassPathXmlApplicationContext(新字符串[] {「applicationContext.testspringsa.services.xml」,「applicationContext-testspringsa-main.xml」}); \t final TestSpringService testSpringService =(TestSpringService)context.getBean(「testSpringService」); \t testSpringService.testIt();' – Meta