SessionFactory始终为null – Spring + Hibernate

我在NullpointerException中的save方法中得到CityDaoImpl 。 似乎sessionFactory没有自动assembly,因为在调试时发现sessionFactory从未在CityDaoImpl注入。 我看了很多答案,但没有一个能解决我的问题。 这是我的HibernateConfig.xml文件:

               com.testing.bean.City     org.hibernate.dialect.MySQLDialect true create true        

这是CityDaoImpl.java

 public class CityDaoImpl implements CityDao{ @Autowired private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory){ this.sessionFactory = sessionFactory; } public void save(City city){ this.sessionFactory.getCurrentSession().save(city); } } 

现在我正在我的dao类上执行unit testing。 但是NPE失败了。

 public class TestCityDao { ApplicationContext ctx; @Before public void setup(){ ctx = new ClassPathXmlApplicationContext("HibernateConfig.xml"); } @Test public void test(){ City city = new City(); city.setName("Karachi"); city.setCountry("Pakistan"); CityDao dao = (CityDao) ctx.getBean("cityDao"); dao.save(city); } } 

控制台输出:

 Jan 15, 2015 11:54:53 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b34bf3: display name [org.springframework.context.support.ClassPathXmlApplicationContext@b34bf3]; startup date [Thu Jan 15 23:54:53 PKT 2015]; root of context hierarchy Jan 15, 2015 11:54:53 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [HibernateConfig.xml] Jan 15, 2015 11:54:53 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@b34bf3]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8202e Jan 15, 2015 11:54:53 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8202e: defining beans [dataSource,sessionFactory,cityDao]; root of factory hierarchy Jan 15, 2015 11:54:53 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO: Loaded JDBC driver: com.mysql.jdbc.Driver SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Jan 15, 2015 11:54:53 PM org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory INFO: Building new Hibernate SessionFactory 

抛出的exception是:

 java.lang.NullPointerException at com.testing.dao.CityDaoImpl.save(CityDaoImpl.java:19) at com.testing.TestCityDao.test(TestCityDao.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

您声明sessionFactory依赖项:

    

然后你还有:

 @Autowired private SessionFactory sessionFactory; 

尝试删除@Autowired注释,因为如果使用XML配置,它是多余的。

仅仅因为你添加了sessionFactory,它并不意味着Spring也可以自动管理Hibernate Sessions。

您还需要添加:

    

以及:

  

现在您还需要在保存例程中添加@Transactional

 @Transactional public void save(City city){ this.sessionFactory.getCurrentSession().save(city); }