Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SessionFactory GetCurrencSession无法使用自动连线_Java_Xml_Spring_Hibernate_Autowired - Fatal编程技术网

Java SessionFactory GetCurrencSession无法使用自动连线

Java SessionFactory GetCurrencSession无法使用自动连线,java,xml,spring,hibernate,autowired,Java,Xml,Spring,Hibernate,Autowired,我想将Hibernate的SessionFactory与Spring一起使用(在手动完成之前) 但我不能在简单的示例中使用SessionFactory 连接到Oracle正常: Jan 01, 2015 5:29:21 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete 还有OK TransactionManager: Jan 01, 2015 5:29:21 PM

我想将Hibernate的SessionFactory与Spring一起使用(在手动完成之前) 但我不能在简单的示例中使用SessionFactory 连接到Oracle正常:

Jan 01, 2015 5:29:21 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
还有OK 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()时,使用

有一个错误:

    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>
<?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>
<?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>
controller.Test.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!");
    }

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

}

Spring自动连接它控制和实例化的bean实例。它不会自动关联您自己创建的对象。当你这样做的时候

new SessionTest();
你不是在向春天要豆子。您正在自己创建一个普通的旧Java对象,而Spring没有意识到这一点,因此无法自动关联此对象的依赖项

您应该从Spring获得SessionTest对象,而不是创建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());
        ...
    }
}

谢谢,你说得对!!!
@Controller
public class Test {

    @Autowired
    private SessionTest sessionTest;

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