我有麻煩在自動綁定我的servlet一些豆類:Servlet的自動裝配失敗
@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
ScoreHandler mScoreHandler;
@Autowired
TransferAdapter mTransferAdapter;
ScoreCreator mScoreCreator;
public void init(ServletConfig config) throws ServletException{
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
如果Spring試圖連線mScoreHandler
我得到這個異常:
org.springframework.beans.factory.BeanCreationException: Injection of autowired dependencies failed for class [class de.bc.qz.server.servlet.ScoreServlet]; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: de.bc.qz.handler.score.ScoreHandler de.bc.qz.server.servlet.ScoreServlet.mScoreHandler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [de.bc.qz.handler.score.ScoreHandler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>quiz-tomcat</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/cmn-dao-spring.xml
classpath*:META-INF/cmn-serv-spring.xml
</param-value>
</context-param>
<servlet>
<servlet-name>ScoreServlet</servlet-name>
<servlet-class>de.bc.qz.server.servlet.ScoreServlet</servlet-class>
</servlet>
</web-app>
這是我的頭像ScoreHandler
:
@Service
public class ScoreHandler {
@Autowired
private ScoreDao mScoreDao;
我的JUnit-Test ScoreHandlerTest
運行良好,沒有問題。我認爲這是Servlet
和Context
的問題。
ScoreHandler放置在它自己的項目中,名爲cmn-server。 這是春天的配置是罐子(CMN-SERV-spring.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<import resource="cmn-dao-spring.xml" />
<bean id="scoreHandler" class="de.bc.qz.handler.score.ScoreHandler"
autowire="byName">
</bean>
</beans>
你能不能幫我看看這個問題?
請發佈'serv-spring.xml'的內容,並告訴我們'ScoreHandler'包的內容。 –
當然...請看我的更新回答。 –
你在使用什麼容器? META-INF真的在類路徑上嗎?沒有META-INF的 –