spring - 使用 Tomcat 使 Spring 与 3 层 Web 系统一起工作

标签 spring jakarta-ee tomcat

我正在为现有的 Java EE 项目添加新功能,该项目涉及三个层:数据、业务和表示。已使用Spring 2.5和JDK 1.4.2。

业务层(域对象项目)使用 Spring 来管理 jdbc 事务并照常注入(inject) DAO 和服务。它有“Spring-config.xml”来配置Spring。它运行良好。

表示层将使用 Spring 2.5、纯 JSP 和 Tomcat 5.0。但是,我坚持设置 web.xml 以在项目启动时加载 Spring。

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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestSpring25Web</display-name>
<listener>
    <description>
  loads the spring application context on startup
</description>
    <display-name>spring application context loader listener</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:web-applicationContext.xml</param-value>
</context-param>
<context-param>
    <param-name>locatorFactorySelector</param-name>
    <param-value>
        classpath:Spring-config.xml
    </param-value>
</context-param>
<context-param>
    <param-name>parentContextKey</param-name>
    <param-value>businessBeanFactory</param-value>
</context-param>
<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>
<!-- DWR -->
<servlet>
    <display-name>DWR Servlet</display-name>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>

Tomcat 5 的错误堆栈跟踪如下:

SEVERE: Context initialization failed
org.springframework.beans.factory.access.BootstrapException: Unable to initialize group     definition. Group resource name [classpath:Spring-config.xml], factory key     [businessBeanFactory]; nested exception is     org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML     document from class path resource [Spring-config.xml]; nested exception is     java.io.FileNotFoundException: class path resource [Spring-config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:385)
at org.springframework.web.context.ContextLoader.loadParentContext(ContextLoader.java:336)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:186)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)

它显然提示找不到“Spring-config.xml”;但是域项目已经被设置为web项目的依赖。

我是 Spring 的新手,但有人可以帮我提供一些提示吗?谢谢

关于像这样配置 3 层 Spring 项目的任何最佳实践?

最佳答案

Spring-config-xml 必须在根类路径中。您没有解释此文件的位置以及如何在 Tomcat 中部署您的应用程序。

关于spring - 使用 Tomcat 使 Spring 与 3 层 Web 系统一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199229/

相关文章:

jsf - 当名为 cid 的查询字符串参数附加到 URL 时,摆脱 org.jboss.weld.context.NonexistentConversationException

java - 作为非阻塞 Java 运行外部进程

json - 如何使用 POST 方法获取 JSON 对象并将其映射到 spring 3.0 Controller 中的对象?

java - 使用 Spring Data 预填充数据

java - 无法实例化[org.springframework.data.domain.Pageable] : Specified class is an interface

java - 无法在 eclipse 中使用 processbuilder 或 runexec 运行命令

Eclipse 和 Tomcat 7

java - Spring Boot - Tiles 问题

java - 如何比较两个包含 Java 中同一类对象的 Arraylists?

jakarta-ee - 我们可以在 web.xml 条目中调用没有 <servlet-mapping> 的 servlet