启动应用程序时出错 - Hibernate + spring mvc

时间:2013-05-18 03:50:55

标签: spring hibernate spring-mvc

我有使用spring mvc + hibernate的应用程序。所有测试它都有效但当我尝试用tomcat启动我的应用程序时出现错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
    org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:156)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:141)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:105)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

web.xml中: ...

<context-param>
        <description>
            Parâmentro de configuração do contexto spring. Local onde se encontram os arquivos .xml do spring
    </description>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*/Spring/*.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <description>Filtro do Spring para uso do Design Pattern "Open Session in View".</description>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<!-- SPRING MVC -->
<!---->
<!-- ############################################################################### -->
<!-- Configurações para o SpringMVC -->
<servlet>
    <servlet-name>SpringMVCServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:*/Spring/springmvc-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <servlet-name>SpringMVCServlet</servlet-name>
</filter-mapping>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <servlet-name>SpringMVCServlet</servlet-name>
</filter-mapping>

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.tag</url-pattern>
        <page-encoding>UTF-8</page-encoding>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
</jsp-config>

...

的applicationContext.xml

...     

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}">
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">
        <list>
            <value>br.com.infowhere.timeSheet.domain</value>
        </list>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven />

...

用SpringMVC-context.xml中

...     

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/Resources/i18n/messages"/>
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>

<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="pt_BR"/>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

...

有人帮帮我吗?

感谢

1 个答案:

答案 0 :(得分:0)

  • 确保这些文件xml文件已正确部署并位于正确的位置 - 即WEB-INF/classes/spring
  • 尝试找出哪个是有问题的定义 - 例如摆脱OpenSessionInViewFilter并查看它是否正常启动。
  • 更喜欢使用小写文件夹名称 - spring而不是Spring(以及applicationContext而不是ApplicationContext)。这不应该干涉,但看起来是反传统的
  • 一般情况下,如果您正确管理实体和/或使用DTO,请避免使用OSIV - 您不需要它。