在Java Spring Hibernate中初始化对象集合

时间:2015-07-18 19:35:16

标签: java spring hibernate initialization

我想创建某种枚举管理器 - 该类应包含数据库中所有对象的集合。
问题:如何初始化该集合?这是我的出发点:

@Component(EnumerationManager.SPORT_ENUM)
@Scope(value = "singleton")

public class SportEnumManager implements EnumerationManager<SportEnum, Long>{

@Autowired
SportEnumDAO gsportEnumDAO;

private Map<Long, SportEnum> objects;
private boolean initialized = false;

@Override
@Transactional
public SportEnum findById(Long id) {
    return objects.get(id);
}   

@PostInitialize
public void init(){
    objects = new HashMap<>();
    List<GenderEnum> list = genderEnumDAO.getAll();
    for (GenderEnum ge : list) {
        objects.put(ge.getId(), ge);
    }
    }

}

问题是sessionFactory在@PostContructor&#34; time&#34;中不可用。

以下是其他配置内容

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">

  <context:component-scan base-package="a.b.c.*" /> 

  <tx:annotation-driven/>

  <mvc:resources mapping="/img/**" location="/img/" />
  <mvc:annotation-driven />


 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/xxxyyy" />
    <property name="username" value="XXX" />
    <property name="password" value="YYY" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource"></property>
     <property name="annotatedClasses">
            <list>

                <value>...</value>                                

            </list>
        </property>
    <property name="hibernateProperties">
      <props>       
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>        
        <prop key="hibernate.show_sql">true</prop>              
        <prop key="hibernate.enable_lazy_load_no_trans">true</prop>                       
      </props>    
    </property>
  </bean>

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

</beans>

1 个答案:

答案 0 :(得分:0)

实际上@postconstruct只能确保在创建bean之后调用方法,但这并不意味着所有其他BeanPostProcessor都已经完成了各自的工作。所以简而言之,它并不能确保容器已准备就绪,这就是为什么会话不可用。已经为此http://jira.springframework.org/browse/SPR-5966添加了jira。

您可以在ContextRefreshedEvent注册ApplicationListener事件来尝试此操作。