gpt4 book ai didi

java - 如何使用 web.xml 和 Tomcat 在 Spring 中正确加载上下文

转载 作者:太空宇宙 更新时间:2023-11-04 14:25:20 25 4
gpt4 key购买 nike

我在 servlet 类中导入 Spring 上下文时遇到问题。这些方法在测试中运行良好(已加载上下文)。但我似乎无法在我的网络应用程序中加载上下文。我使用的是 Tomcat 7。这是我的 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>AgneseProva</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>Prova</display-name>
<servlet-name>Prova</servlet-name>
<servlet-class>it.jenia.agnese.prova.Prova</servlet-class>
</servlet>
<servlet>
<description></description>
<display-name>Presenze</display-name>
<servlet-name>Presenze</servlet-name>
<servlet-class>it.jenia.agnese.prova.Presenze</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Prova</servlet-name>
<url-pattern>/Prova</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Presenze</servlet-name>
<url-pattern>/Presenze</url-pattern>
</servlet-mapping>
</web-app>

这是我的应用程序上下文app-config.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<import resource="classpath*:/test-prova-spring-configuration.xml" />
</beans>

文件 test-prova-spring-configuration.xml 包含一系列数据库配置、存储库配置、实体和服务的导入。

Servlet 类是:

 package it.jenia.agnese.prova;

import it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository;
import it.jenia.ac.mail.rapportini.repository.PresenzaRepository;
import it.jenia.vr.common.service.JspReporter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

public class Presenze extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(Presenze.class);

@Autowired
CollaboratoreRepository collaboratoreRepository;
@Autowired
PresenzaRepository presenzaRepository;
@Autowired
JspReporter jsp;

public Presenze() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String messaggiojsp = "ok";
String messaggiopr = "ok";
String messaggiocr = "ok";
if (jsp==null){
messaggiojsp = "ko";
}
request.setAttribute("messaggiojsp", messaggiojsp);
if(presenzaRepository ==null){
messaggiopr = "ko";
}
request.setAttribute("messaggiopr", messaggiopr);
if(collaboratoreRepository==null){
messaggiocr = "ko";
}
request.setAttribute("messaggiocr", messaggiocr);
request.getRequestDispatcher("/Presenze/presenze.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

服务器不会抛出任何错误,就像上下文已完全加载一样,但 servlet 中的 Autowiring 字段为空。到目前为止我已经尝试过:

1.使用@Service注释将servlet注册为服务,并将其包添加到spring配置文件(test-prova-spring-configuration.xml)中的组件扫描中-->没有任何改变,字段为空并且Tomcat没有抛出任何错误。

2.在 spring 配置文件中将 servlet 注册为 bean --> 没有任何更改,字段为 null 并且 Tomcat 没有引发任何错误。

3.将这三行代码添加到 servlet 中:

public void init(javax.servlet.ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
};

--> 页面抛出错误(最后):

org.springframework.beans.factory.BeanCreationException:类 [class it.jenia.agnese.prova.Presenze] 的 Autowiring 依赖项注入(inject)失败;

回溯:

    exception

javax.servlet.ServletException: Servlet.init() for servlet Presenze threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

org.springframework.beans.factory.BeanCreationException: Injection of autowired dependencies failed for class [class it.jenia.agnese.prova.Presenze]; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository it.jenia.agnese.prova.Presenze.collaboratoreRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository] 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)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.processInjection(AutowiredAnnotationBeanPostProcessor.java:306)
org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(SpringBeanAutowiringSupport.java:110)
it.jenia.agnese.prova.Presenze.init(Presenze.java:45)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

org.springframework.beans.factory.BeanCreationException: Could not autowire field: it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository it.jenia.agnese.prova.Presenze.collaboratoreRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository] 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)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.processInjection(AutowiredAnnotationBeanPostProcessor.java:303)
org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(SpringBeanAutowiringSupport.java:110)
it.jenia.agnese.prova.Presenze.init(Presenze.java:45)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.jenia.ac.mail.rapportini.repository.CollaboratoreRepository] 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)}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.processInjection(AutowiredAnnotationBeanPostProcessor.java:303)
org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(SpringBeanAutowiringSupport.java:110)
it.jenia.agnese.prova.Presenze.init(Presenze.java:45)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

但是,总而言之,问题在于它无法在 servlet 类中找到用于 Autowiring 存储库的 bean。

我已经完成了我的“新手”资源,因此我们将不胜感激。

PS。 o 我忘记了我有一个 Maven Web 应用程序,目录结构是:

src
---main
------java
----------it.jenia.agnese.prova
---------------Presenze.java
------resources
------webapp
----------META-INF
--------------test-prova-spring-configuration.xml
--------------MANIFEST.MF
----------WEB-INF
--------------web.xml
--------------spring
------------------app-config.xml
----------Presenze
--------------presenze.jsp

最佳答案

问题出在配置文件中。我尝试移动 META-IN 目录,但没有成功,然后我更改了这一行:

<import resource="classpath*:/test-prova-spring-configuration.xml" /> 

至:

<import resource="classpath*:/META-INF/test-prova-spring-configuration.xml" /> 

突然,服务器 Tomcat 为我提供了一些关于配置中缺少的类的输出(它们包含在我的 maven pom.xml 中,但被标记为测试,因为我之前仅将配置用于测试范围)。当我从 pom.xml 中取消测试范围时,每个类都工作正常。我只需更新 Maven 项目并清理 tomcat 项目即可使其正常工作。所以这是“类路径”不被识别的问题。奇怪的是,Tomcat 没有给出任何有关找不到 xml 的输出。应该有一种方法来记录配置加载过程来解决这个问题。我从这个问题的回答中得到了一些启发Printing path of spring.xml in classpath .

关于java - 如何使用 web.xml 和 Tomcat 在 Spring 中正确加载上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738219/

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