0

我已经分离了一个 Spring maven 项目,myapp.web仅包含 JSP 和静态文件,而 myapp.core 包含应用程序上下文 XML 和控制器。

myapp.core

-- src/main/resources/context.xml

-- src/main/resources/mvc-context.xml

myapp.web

-- src/main/webapp/WEB-INF/web.xml

web.xml 包含根上下文配置和 servlet 配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    ...

<servlet>
    <servlet-name>servlet0</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/mvc-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>servlet0</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我也OpenSessionInViewFilter配置了

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
        <param-name>flushMode</param-name>
        <param-value>AUTO</param-value>
    </init-param>

</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>  

现在,从myapp.web生成战争并手动放入 Tomcat 可以正常工作。但是,当通过“在服务器上运行”从 Eclipse 运行时,OpenSessionInViewFilter看不到sessionFactorycontext.xml 中配置了哪个我尝试修复项目的构建路径,但它不会运行。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1094)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:242)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:227)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171)

现在我知道这是一个Eclipse/m2e-wtp配置问题,因为它是通过手动部署工作的,但我一直无法修复它。按照通常建议的方式使用项目的构建路径,但没有成功。

谢谢

4

2 回答 2

0

通过 Eclipse 运行应用程序时,检查 myapp.core 中的 xml 文件是否位于类路径中。他们很可能不是。您可以使用类似的东西进行检查

if (getClass().getResource("my context-xml file") ! = null) {
   System.out.println("No context file on the classpath, won't work");
}

问题可能是 Eclipse 未配置为将 xml 文件复制到 bin 目录。

于 2014-06-10T09:45:53.607 回答
0

解决了。问题是这样的:上下文是自动重新加载的(不知道为什么)!myapp.core曾经是入口点(网络应用程序)。拆分后,它在名为Utility Project的Project Facets上缺少一个刻度,并且 Maven Integration for Eclipse 没有将此项目部署为 jar!

于 2014-06-13T11:44:55.253 回答