SessionFactory getCurrencSesion无法使用自动装配

时间:2015-01-01 15:38:57

标签: java xml spring hibernate autowired

我想在Spring中使用Hibernate的SessionFactory(在我手动完成之前) 但是我不能在简单的例子中使用SessionFactory 连接到Oracle是可以的:

Jan 01, 2015 5:29:21 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete

也好的TransactionManager:

Jan 01, 2015 5:29:21 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.apache.tomcat.dbcp.dbcp.BasicDataSource@c5fac0] of Hibernate SessionFactory for HibernateTransactionManager

但是当我尝试使用

获取getCurrentSession()时
SessionTest sessionTest = new SessionTest();
System.out.println("getSessionFactory() " + sessionTest.getSession());

有错误:

    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at test.SessionTest.getSession(SessionTest.java:17)
    at controller.Test.index(Test.java:25)

我认为autowired不适用于test.SessionTest.java

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/test/</url-pattern>
    </servlet-mapping>

<listener>
   <listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> 
</web-app>

调度-servlet.xml中

<?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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-3.0.xsd 
            http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
             http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="controller" />

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

</beans>

的applicationContext.xml

<?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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-3.0.xsd 
            http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
             http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>test.SessionTest</value>

            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.globally_quoted_identifiers">true</prop>


            </props>
        </property>

    </bean>

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@my.adgawegaw.us-east-1.rds.amazonaws.com:1521:ORCL" />
        <property name="username" value="*****" />
        <property name="password" value="*****" />

    </bean>


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

test.SessionTest.java

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class SessionTest {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
    }

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

    public SessionTest() {
        System.out.println("Test created!");
    }

}

controller.Test.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import test.SessionTest;

@Controller
public class Test {

    @RequestMapping("/test/")
    public ModelAndView index( //
            ModelMap map, Model m //
    ) {


        ModelAndView mav = new ModelAndView();
        mav.setViewName("test");


        SessionTest sessionTest = new SessionTest();
        System.out.println("getSessionFactory() " + sessionTest.getSession());


        return mav;
    }

}

1 个答案:

答案 0 :(得分:2)

Spring自动装配它控制和实例化的bean实例。它不会自动装配您自己创建的对象。当你这样做

new SessionTest();
你不是要问春天的豆子。你自己创建了一个普通的旧Java对象,而Spring并没有意识到它,因此无法自动装配这个对象的依赖。

您应该从Spring获取它,而不是创建SessionTest对象。由于Spring是实例化您使用SessionTest的控制器的一个,因此您可以在控制器内自动装配SessionTest:

@Controller
public class Test {

    @Autowired
    private SessionTest sessionTest;

    @RequestMapping("/test/")
    public ModelAndView index(ModelMap map, Model m) {
        System.out.println("getSession() " + sessionTest.getSession());
        ...
    }
}