No bean named 'MysessionFactory' is defined
  TEZNKK3IfmPf 2023年11月15日 30 0

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>
<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>


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   19天前   43   0   0 java
TEZNKK3IfmPf