gpt4 book ai didi

java - Servlet Autowiring 失败

转载 作者:行者123 更新时间:2023-11-30 04:07:14 26 4
gpt4 key购买 nike

我在 servlet 中 Autowiring 一些 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 配置(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* 前缀有如下说法

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then merged to form the final application context definition.

如果没有找到任何内容,则不会使用(合并)任何内容。 This is explained here.

就您的情况而言,我们可以安全地假设 META-INF 不在类路径上(而且可能不应该在类路径上),因此找不到文件,也不会生成任何 bean。

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

关于java - Servlet Autowiring 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20455313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com