SSH整合 出现 No bean named ‘MysessionFactory’ is defined
HTTP Status 500 - No bean named 'MysessionFactory' is defined type Exception report message No bean named 'MysessionFactory' is defined description The server encountered an internal error that prevented it from fulfilling this request. exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MysessionFactory' is defined org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698) org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201) org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051) org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:190) org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:175) org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:126) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs. Apache Tomcat/7.0.42
Spring配置如下:
<!-- 注册SessionFactory --> <bean id="MySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置映射文件所在文件夹 --> <property name="mappingDirectoryLocations" value="classpath:com/hk/beans"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean>
web.xml配置如下:
<filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
原因:
在源码中可以看到:
public class OpenSessionInViewFilter extends OncePerRequestFilter { public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory"; private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME; /** * Set the bean name of the SessionFactory to fetch from Spring's * root application context. Default is "sessionFactory". * @see #DEFAULT_SESSION_FACTORY_BEAN_NAME */ public void setSessionFactoryBeanName(String sessionFactoryBeanName) { this.sessionFactoryBeanName = sessionFactoryBeanName; }
OpenSessionInViewFilter 中对`SessionFactoryBeanName`有个默认的名字`sessionFactory`,所以如果你的名字不是`sessionFactory`的话就找不到
解决方案:再看一下OpenSessionInViewFilter
源码,发现SessionFactoryBeanName
是一个属性名,同时还提供了set方法,这也就是说我们可以设置自己的SessionFactoryBeanName
——如下:
<filter> <filter-name>openSessionInView</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>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>