Spring + Hibernate配置,空SessionFactory

时间:2014-08-20 15:53:50

标签: spring hibernate nullpointerexception spring-annotations sessionfactory

我已经进行了数小时的广泛搜索,看到了针对特定/常见问题的不同方法的不同场景,但没有运气来解决一个非常简单的Null SessionFactory。这是我的课程,我尽量缩短,所以当我试图减少代码时,我希望找到问题所在。仍然没有运气。

服务类

public class Service {

  @Autowired
  SessionFactory sessionFactory;

  @Transactional
  public void doSomething() {

     Student stud = new Student();

     stud.setName("Student");
     stud.setAge(23);
     stud.setSchool("School");

     System.out.println(sessionFactory);

     sessionFactory.getCurrentSession().save(stud);
   }
}

我的实体类(POJO)

@Entity
@Table(name = "STUDENT")
public class Student extends Person {

  @Column(name = "school")
  private String school;

  public Student() {
  }

  public Student(String school) {
     this.school = school;
  }

  public String getSchool() {
    return school;
  }

  public void setSchool(String school) {
    this.school = school;
  }
}

配置文件(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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<tx:annotation-driven/>

<context:property-placeholder
    location="classpath:properties/database.properties"
    ignore-unresolvable="false" />

<context:component-scan base-package="edu.service"/>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>edu.domain</value>
        </list>
    </property>
</bean>

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

主要课程

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
            "/config/applicationContext.xml");

      Service service = new Service();
      service.doSomething();

      ((ClassPathXmlApplicationContext) context).registerShutdownHook();
      ((ClassPathXmlApplicationContext) context).close();
  } 
}

我真的不明白为什么我有一个空的SessionFactory,我试图让它尽可能短,删除了部分应用程序(DAO,服务,注释),仍然没有运气。 请帮忙。提前谢谢

2 个答案:

答案 0 :(得分:0)

问题在于new Service()所以你创建的对象不是由Spring管理的。您从未调用setSessionFactory(),因此SessionFactory当然是null,因此当您尝试引用它时就是NPE。

您可能希望获得由Spring容器管理的Service,这意味着您需要以某种方式注入它。 因此,尝试注入bean Service,然后调用service.doSomething();

答案 1 :(得分:0)

试试这个:

首先在xml中注释或配置服务。 然后从下面的上下文中获取它:

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "/config/applicationContext.xml");

    Service service = (Service) context.getBean("service");
    service.doSomething();

    ((ClassPathXmlApplicationContext) context).registerShutdownHook();
    ((ClassPathXmlApplicationContext) context).close();
}

如果有帮助,请告诉我。