我有一个分离的Spring maven项目,myapp。web只包含jsp和静态文件,而myapp。core包含应用上下文xml和控制器。
myapp.core
, src/主/资源/context.xml
, src/主/资源/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生成war。web和drop在Tomcat中手动工作很好。但是,当通过"在服务器上运行"从Eclipse运行时,OpenSessionInViewFilter
看不到在context.xml中配置的sessionFactory
我试着修复项目的构建路径,但它不会运行。
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配置问题,因为它通过手动部署工作,但我一直无法修复它。按照通常的建议,使用项目的Build Path,但没有成功。
谢谢
检查myapp中的xml文件。当您通过eclipse运行应用程序时,内核位于类路径中。很可能不是。您可以使用
之类的内容进行检查if (getClass().getResource("my context-xml file") ! = null) {
System.out.println("No context file on the classpath, won't work");
}
问题可能是Eclipse配置为不将xml文件复制到bin目录
解决。问题是:上下文是自动重新加载的(不知道为什么)! myapp。core曾经是入口点(web应用程序)。在拆分之后,它在Project Facets上缺少一个标记,称为Utility Project,并且Eclipse的Maven Integration没有将该项目部署为jar!