本文介绍了SessionFactory注入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SessionFactory没有被注入SessionFactory变量。我的配置如下:

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:context =http://www.springframework.org/schema/context
xmlns:mvc =http://www.springframework.org/schema/mvc
xmlns :tx =http://www.springframework.org/schema/tx
xsi:schemaLocation =
http://www.springframework.org/schema/mvc
http: //www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/ schema / beans / spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context- 3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd\">

< mvc:annotation-driven />

< tx:annotation-driven />

< context:annotation-config />

< context:component-scan base-package =com.csu.library.mvc/>

< mvc:resources location =/ resources /mapping =/ resources / **/>

< bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =viewClassvalue =org.springframework.web.servlet.view.JstlView/>
< property name =prefixvalue =/ WEB-INF / jsps //>
< property name =suffixvalue =.jsp/>
< / bean>

< bean id =dataSourcedestroy-method =closeclass =org.apache.commons.dbcp.BasicDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / csu_library/>
< property name =usernamevalue =csulibrary/>
< property name =passwordvalue =csulibrary/>

< property name =initialSizevalue =5/>
< property name =maxIdlevalue =5/>
< property name =maxActivevalue =10/>
< / bean>

< bean id =hibernatePropertiesclass =java.util.Properties>
< constructor-arg index =0>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< prop key =hibernate.cglib.use_reflection_optimizer> true< / prop>
< prop key =hibernate.cache.provider_class> org.hibernate.cache.HashtableCacheProvider< / prop>
< /道具>
< / constructor-arg>
< / bean>

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =packagesToScanvalue =com.csu.library.mvc/>
< property name =dataSourceref =dataSource/>
< property name =hibernatePropertiesref =hibernateProperties/>

< / bean>

< bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< / beans>

HibernateUtil.class

  package com.csu.library.mvc.hibernate; 

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@ org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class HibernateUtil {

@Autowired
@Qualifier( sessionFactory)
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory(){
return sessionFactory;


$Be
public HibernateTransactionManager transactionManager(){
return new HibernateTransactionManager(getSessionFactory());


$Be
public HibernateExceptionTranslator exceptionTranslator(){
return new HibernateExceptionTranslator();


程序抛出 NullPointerException 当调用getSessionFactory()方法时。显然,sessionFactory没有被注入。该程序启动正常。可能是这样的问题?

解决方案

注入如下,它会正常工作

  @Autowired 
@Qualifier(sessionFactory)
private SessionFactory sessionFactory;

public Session getSession(){
return sessionFactory.getCurrentSession();
}

希望能帮到你:

My SessionFactory isn't being injected to SessionFactory variable. My configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <mvc:annotation-driven/>

    <tx:annotation-driven/>

    <context:annotation-config/>

    <!-- Scans the package for contents -->
    <context:component-scan base-package="com.csu.library.mvc"/>

    <!-- Maps static resources like images, css, javascript files -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>

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

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/csu_library"/>
        <property name="username" value="csulibrary"/>
        <property name="password" value="csulibrary"/>

        <!-- Pool Properties -->
        <property name="initialSize" value="5" />
        <property name="maxIdle" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id = "hibernateProperties" class = "java.util.Properties">
        <constructor-arg index="0">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value = "com.csu.library.mvc"/>
        <property name="dataSource" ref = "dataSource"/>
        <property name="hibernateProperties" ref = "hibernateProperties"/>

    </bean>

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

HibernateUtil.class

package com.csu.library.mvc.hibernate;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class HibernateUtil {

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(getSessionFactory());
    }

    @Bean
    public HibernateExceptionTranslator exceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
}

The program throws NullPointerException when getSessionFactory() method is called. Obviously, sessionFactory isn't being injected. The program starts up fine. What could be the problem?

解决方案

inject like following, it'll work fine

@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

public Session getSession() {
    return sessionFactory.getCurrentSession();
}

Hope to help you:)

这篇关于SessionFactory注入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:04