【问题标题】:Servlet Autowire failsServlet 自动装配失败
【发布时间】:2013-12-25 15:48:24
【问题描述】:

在我的 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>

你能帮我找出问题吗?

【问题讨论】:

  • 请把serv-spring.xml的内容贴出来,告诉我们ScoreHandler在哪个包里。
  • 当然...查看我的更新答案。
  • 您使用的是什么容器? META-INF 真的在类路径上吗?
  • 没有 META-INF 我在包含 jar 中的 xml 文件时遇到了麻烦。使用 META-INF,我的本地 tomcat 的启动工作正常。
  • 在tomcat找不到xml之前

标签: java spring servlets autowired


【解决方案1】:

错误隐藏在

<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(...) 调用),然后合并形成 最终应用上下文定义。

如果它没有找到任何东西,它就不会使用(合并)任何东西。 This is explained here.

在您的情况下,我们可以安全地假设 META-INF 不在类路径中(它可能不应该在),因此没有找到文件,也没有生成任何 bean。

我会声明一个自定义 Spring 上下文配置文件并将其添加到您的类路径中。不要依赖其他罐子。

【讨论】:

  • @Stefan 这些 jars 将被添加到类路径中,但我不知道 ClassLoader 如何处理 META-INF 文件夹。尝试搜索如何从外部资源添加上下文文件。
猜你喜欢
  • 2018-02-21
  • 2012-03-19
  • 2016-01-21
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
  • 2017-05-08
  • 2012-11-20
相关资源
最近更新 更多