2013-04-04 66 views
1

我的SessionFactory未被注入SessionFactory變量。我的配置如下:SessionFactory注入不起作用

<?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(); 
    } 
} 

程序拋出的NullPointerException時了getSessionFactory()方法被調用。顯然,sessionFactory沒有被注入。該程序啓動正常。可能是什麼問題呢?

+0

而且您確定您的Util類位於com.csu.library.mvc目錄中? – javadev 2013-04-04 11:10:10

+0

我這麼認爲。 Util類的包名是com.csu.library.mvc.hibernate – 2013-04-04 11:15:09

+0

在您的項目中如何創建Util類bean? – 2013-04-04 11:33:51

回答

2

注入像下面,它會正常工作

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

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

希望能幫助你:)

+0

仍然拋出NullPointerException – 2013-04-04 11:56:20

+1

請仔細看看我的代碼,SessionFactory的變量不是靜態的。那麼你知道該怎麼辦? – 2013-04-04 12:07:40

+0

我試圖進行更改。我已經從方法和變量中刪除了「靜態」,但仍然是sessionFactory爲空。 請參閱更新的HibernateUtil類。 – 2013-04-04 12:53:31

0

在某些Spring版本中,不掃描子文件夾。 要麼將​​您的類移動到包com.csu.library.mvc中,要麼在掃描的包中添加com.csu.library.mvc.hibernate包,然後試一試。

+0

嘗試失敗。 – 2013-04-04 11:23:34

+0

添加packege和移動類都失敗了嗎? – javadev 2013-04-04 11:25:21

+0

我將前面提到的軟件包添加到了base-package屬性中。沒有工作。 – 2013-04-04 11:30:14

0

你要使用org.springframework.beans.factory.annotation.Autowired註釋,而不是javax.inject.Inject

@Autowired 
private SessionFactory sessionFactory; 
+0

我用Autowired替換了Inject。仍然不起作用。 – 2013-04-04 12:01:21

0

注意,只查找在它的定義相同 應用程序上下文豆的註解。這意味着,如果你把一個 WebApplicationContext中的DispatcherServlet的,它僅適用於@Autowired豆檢查你的 控制器,而不是你的服務。