我正在使用Spring MVC,我想初始化一个spring bean,在这个init期间我想从数据库中获取数据。
[20 / mars / 15 10:54:44:044 GMT + 01:00] ERROR context.ContextLoader: 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:错误 创建名为'employeeMB'的bean:调用init方法 失败;嵌套异常是org.hibernate.HibernateException:没有 找到当前线程的会话 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1505)
引起:org.hibernate.HibernateException:找不到任何会话
的当前主题org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) 在 org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024) 在 com.tds.erp.dao.impl.EmployeeDaoImpl.listEmployee(EmployeeDaoImpl.java:47) 在 com.tds.erp.services.impl.EmployeeServiceImpl.listEmployee(EmployeeServiceImpl.java:48) at com.tds.erp.managedController.EmployeeMB.init(EmployeeMB.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
的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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<context:component-scan base-package="com.tds.erp" />
<context:annotation-config />
<context:spring-configured />
<!-- Data Source Declaration -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.tds.erp.model" />
<!-- <property name="packagesToScan"> <list> <value>net.javabeat.spring.model</value>
</list> </property> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<!-- <tx:annotation-driven transaction-manager="txManager"/> -->
<!-- Transaction Manager is defined -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>
ManagedBean.java
@Component
@ManagedBean
@SessionScoped
public class EmployeeMB {
@Autowired
private IEmployeeService EmployeService;
private Employee employee= new Employee();
private List<Employee> employeeList= new ArrayList<Employee>();
private Employee selectedEmployee=new Employee();
private boolean headerButtonsDisabled=true;
@PostConstruct
public void init(){
setEmployeeList(EmployeService.listEmployee());
}
public void setEmployeService(IEmployeeService employeService) {
EmployeService = employeService;
}
public IEmployeeService getEmployeService() {
return EmployeService;
}
public Employee getEmployee() {
return employee;
}