簡體   English   中英

Servlet自動連線失敗

[英]Servlet Autowire fails

我無法在servlet中自動裝配一些bean:

@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運行得很好,沒有問題。 我認為這是ServletContext

ScoreHandler放置在其自己的名為cmn-server的項目中。 這是該jar的spring config(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>

你能幫我找到問題嗎?

錯誤隱藏在

<param-value>
    classpath*:META-INF/cmn-dao-spring.xml
    classpath*:META-INF/cmn-serv-spring.xml
</param-value>

如果刪除* ,則會得到一個

Caused by: java.io.FileNotFoundException: class path resource [cmn-serv-spring.xml] cannot be opened because it does not exist

Spring說了以下有關classpath*前綴的內容

這個特殊的前綴指定必須獲取與給定名稱匹配的所有類路徑資源(內部,這實際上是通過ClassLoader.getResources(...)調用發生的),然后合並以形成最終的應用程序上下文定義。

如果找不到任何內容,則不使用(合並)任何內容。 在這里解釋。

在您的情況下,我們可以安全地假定META-INF不在類路徑上(並且可能不應該在類路徑上),因此沒有找到文件,也沒有生成bean。

我將聲明一個自定義的Spring上下文配置文件,並將其添加到您的類路徑中。 不要依賴其他罐子。

暫無
暫無

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

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