【问题标题】:spring dependencies in my project hierarchy我的项目层次结构中的弹簧依赖项
【发布时间】:2013-12-25 21:38:11
【问题描述】:

我整天都在解决这个问题。 我有一个具有这种结构的工作区:

  • cmn-lib(常用基本算法)#Java
  • cmn-server(基于通用服务器的逻辑)#Java
  • cmn-dao(数据库接口)#Java
  • qz-tomcat(tomcat项目)#Java
  • qz-client(客户端)#Android

cmn-server 以及 cmn-dao 使用 Spring(测试运行没有问题)。 cmn-server-spring.xml的spring配置包含common-dao-spring.xml(因为有些Handler类需要Dao支持)。 这是 cmn-server-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>

现在我想将所有这些库包含到 qz-tomcat 中。 问题是那个异常:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [cmn-dao-spring.xml]
Offending resource: URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-serv-spring.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-dao-spring.xml]; nested exception is java.io.FileNotFoundException: JAR entry cmn-dao-spring.xml not found in C:\Users\BC\qz\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\quiz-tomcat\WEB-INF\lib\cmn-server.jar
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)

它发生在我启动本地 tomcat 时。 cmn-server 和 cmn-dao 在“Web 部署程序集”的帮助下作为 JAR 包含在内。

但是...我的 webapp 在SpringBeanAutowiringSupport 期间崩溃了:

@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());
    }

我的 cmn-server.jar 有问题吗? 我认为主要问题是异常中的那一行:

IOException parsing XML document from URL [jar:file:/C:/Users/BC/qz/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quiz-tomcat/WEB-INF/lib/cmn-server.jar!/cmn-dao-spring.xml

我的 cmn-server.jar 中没有 common-dao-spring.xml。我已通过Java Build Path-&gt;Project-&gt;Add-&gt;cmn-dao 将项目 cmn-dao 添加到 cmn-server 该配置似乎适用于 JUnit 测试,但不适用于已部署的 Jar-File。

知道任何人如何解决这个问题。

感谢您的每一个帮助。

【问题讨论】:

    标签: java spring tomcat jar


    【解决方案1】:

    您应该将 Spring 配置文件保存在它们所属的 jar 中,使用标准的 META-INF/spring 位置也不错。您要做的是告诉 Spring 在类路径中查找配置文件:

    <import resource="classpath:[/META-INF/spring]/cmn-dao.xml" />
    

    另外,请注意,您的命名显然不匹配,这可能是真正破坏运行时的唯一问题:您不断在 cmncommon 之间来回切换,并且有或没有 spring 在结束。选择一种约定并使用它。

    【讨论】:

    • 所以您的意思是我从 cmn-server-spring.xml 中删除 dao-reference 并通过您的示例确定位置?你有教程的参考链接吗?命名的区别仅在我的问题中......
    • 请注意,META-INF 通常不在类路径中。
    • @StefanBeike 您评论中的问题不清楚。那么解决你的问题;不要发布“这有点像我的真正问题”代码。我是说您使用classpath: 告诉Spring 在类路径的另一个jar 中查找导入的上下文XML 文件。所有其他常规规则均适用。
    • @SotiriosDelimanolis 不确定“通常不在类路径上”是什么意思;通常的做法是将 Spring 配置文件放在 META-INF/spring (在 src/main/resources 或其他)中,并且将它们打包在 jar 中。 classpath: 指令告诉 Spring 在不同的类路径选项中查找该文件,例如在库 jar 中。
    • 在 Web 应用程序 ex. tomcat 中,只有 WEB-INF/classes 下的文件/类和 WEB-INF/lib 中的 jars 会放在类路径中。因此,如果 jar 包含 META-INF/wtv 文件,这将起作用,但如果 META-INF 在 Web 应用程序本身中,则不会。
    猜你喜欢
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多