我的项目层次结构中的spring依赖项

时间:2013-12-08 21:37:05

标签: java spring tomcat jar

我整天都在研究这个问题。 我有一个带有这种结构的工作区:

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

cmn-server以及使用Spring的cmn-dao(测试运行没有问题)。 cmn-server-spring.xml的spring配置包括common-dao-spring.xml(Becouse一些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时会发生这种情况。 在“Web部署程序集”的帮助下,cmn-server和cmn-dao作为JAR包含在内。

然而......我的网络应用程序在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->Project->Add->cmn-dao将项目cmn-dao添加到cmn-server 该配置似乎适用于JUnit测试,但不适用于部署的Jar-File。

知道如何解决这个问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您应该将Spring配置文件保存在它们所属的jar中,并且使用标准META-INF/spring位置并不是一个坏主意。你想要做的是告诉Spring在类路径上查找配置文件:

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

另外,请注意,您的命名显然不匹配,这可能是实际破坏运行时的唯一问题:您经常在cmncommon之间来回切换,是否拥有最后spring。选择一个约定并使用它。