原因是在web.xml中配置了OpenSessionInViewFilter。但是在访问系统的时候抛出了这个异常:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined。原因是OpenSessionInViewFilter这个类中有个属性叫 sessionFactoryBeanName默认值为sessionFactory,而我的spring配置文件中sessionFactory不叫这个名字,修改名字后正常。
spring配置文件。
<!--配置hibernate的相关属性 -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate 的基本属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!-- 加载 hibernate 映射文件 (可以使用通配符) -->
<property name="mappingLocations" value="classpath:/com/oc/beans/*.hbm.xml"></property>
</bean>
web.xml配置文件。
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mySessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
param-value的值应该和bean中id的值对应。