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

Initialize collection of objects in Java Spring Hibernate

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

@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 中不可用 "time"。

这是其他配置内容

    <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>

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

添加会话 available.Already jira 的原因

您可以通过在 ApplicationListener 中注册 ContextRefreshedEvent 事件来尝试。