我在servlet类中导入Spring上下文时遇到问题。这些方法在测试中运行良好(加载了上下文)。但我似乎无法在我的webapp中加载上下文。我使用的是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中的自动装配字段为空。到目前为止,我已经尝试过了:
1.使用@Service注释将servlet作为服务注册,并将其添加到spring配置文件中的组件扫描中(test-prova-spring-configuration.xml) - &gt;没有改变,字段为空,Tomcat没有任何错误。
2.在弹簧配置文件中将servlet注册为bean - &gt;没有改变,字段为空,Tomcat没有任何错误。
3.将这三行代码添加到servlet中:
public void init(javax.servlet.ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
};
- &GT;页面抛出错误(最后):
org.springframework.beans.factory.BeanCreationException:为类[class it.jenia.agnese.prova.Presenze]注入自动连接的依赖项失败;
回溯:
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)
然而,总结一下,问题是它无法找到bean来自动装配servlet类中的repo。
我完成了我的&#34;新手&#34;资源,所以任何帮助将不胜感激..
PS。我忘了我有一个maven webapp,dir结构是:
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
答案 0 :(得分:0)
问题出在配置文件中。我尝试移动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项目以使其工作。所以这是&#34; classpath&#34;的一个问题。不被承认。奇怪的是Tomcat没有给出关于xml没有找到的任何输出。应该有一种方法来记录配置加载过程来修复它。我从这个问题Printing path of spring.xml in classpath的答案中得到了一些启发。