Spring + Hibernate + DAO + SessionFactory + Inject

Я использую Spring с Hibernate и всегда получаю NPE для объекта sessionFactory.

Мой файл конфигурации:

@Configuration
public class HibernateConfiguration {

@Bean
public AnnotationSessionFactoryBean sessionFactory() {
    Properties props = new Properties();
    props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
    props.put("hibernate.format_sql", "true");
    props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    props.put("hibernate.connection.password", "xxx");
    props.put("hibernate.connection.url", "jdbc:mysql://localhost/Market");
    props.put("hibernate.connection.username", "philipp");

    AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
    bean.setAnnotatedClasses(new Class[] { xxx.class, xxx.class, xxx.class });
    bean.setHibernateProperties(props);
    bean.setSchemaUpdate(true);
    return bean;
}





@Bean
public HibernateTransactionManager transactionManager() {
    return new HibernateTransactionManager(sessionFactory().getObject());
}

@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
    return new PersistenceExceptionTranslationPostProcessor();
}

}

Мой класс DAOImpl:

@Repository("xxx")
public class xxxDAOImpl implements xxxDAO {

private SessionFactory sessionFactory;

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

private Session currentSession() {
    return sessionFactory.getCurrentSession();
}
...

Прецедент:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class TestxxxDAOImpl {

@Test
@Transactional
public void testInsertxxx() throws Exception {

    xxxDAO xxxDAO = new xxxDAOImpl();
    xyz xyz = new xyz();
    xxxDAO.insert(xyz);
    assertNotNull(xyz.getId());
    }

}

приложение-контекст.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
    http://www.springframework.org/schema/jdbc   http://www.springframework.org/schema/jdbc/spring-jdbc-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">


<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="xxx.config" />
<context:component-scan base-package="xxx.dao" />
<context:annotation-config></context:annotation-config>

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

<import resource="classpath:/META-INF/spring/app-context.xml" />

I get always an NPE from the currentSession when the test calls the insert method.

public void insert(xxx xxx) {
    currentSession().save(xxx);
}


private Session currentSession() {
    return sessionFactory.getCurrentSession();
}

person Philippxp    schedule 06.06.2012    source источник


Ответы (2)


Самое первое, что нужно понять при работе со Spring, это то, что внедрение зависимостей Spring работает только для bean-компонентов, полученных из контекста приложения, а не для bean-компонентов, созданных с помощью new.

В модульных тестах с поддержкой Spring вы настраиваете контекст приложения с помощью @ContextConfiguration, например, следующим образом (работает в Spring 3.1, в предыдущих версиях @ContextConfiguration не использует классы @Configuration напрямую, поэтому вам придется создать файл конфигурации XML):

@Configuration 
public class HibernateConfiguration { 
    // Add your DAO to application context
    @Bean
    public xxxDAO xxxDAO() {
        return new xxxDAOImpl();
    }
    ...
}

// Configure application context using the given @Configuration class
@ContextConfiguration(classes = HibernateConfiguration.class)   
@RunWith(SpringJUnit4ClassRunner.class)
public class TestxxxDAOImpl {
    // Obtain DAO from the application context    
    @Autowired xxxDao xxxDao;
    ...
}
person axtavt    schedule 06.06.2012
comment
Это не будет работать с более чем одной конфигурацией. Autowired не имеет названия. - person mmm; 21.01.2021

Вместо sessionFactory.getCurrentSession() в функции currentSession() класса xxxDAOImpl используйте sessionFactory.openSession().

Поскольку при первом вызове в фабрике сеансов не будет текущего сеанса, вам нужно открыть сеанс.

Надеюсь, это поможет вам. Ваше здоровье.

person Japan Trivedi    schedule 06.06.2012
comment
Следует ввести sessionFactory, поэтому у меня всегда есть сеанс в моем классе xxxDAOImpl. Проблема в том, что sessionFactory не вводится Spring. - person Philippxp; 06.06.2012
comment
Я рекомендую вам проверить точку NPE еще раз должным образом. Я подозреваю, что проблема в getCurrentSession, как я уже сказал. Пожалуйста, покажите здесь свою трассировку стека, чтобы я мог хорошо понять. - person Japan Trivedi; 06.06.2012