package com.zhongdao.jlr.business.listener;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.orm.hibernate4.SessionHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * 启动监听器
 * 
 * @author Storezhang
 */
@Component
public class StartupListener implements
		ApplicationListener<ContextRefreshedEvent> {
	@Resource  
	 private SessionFactory sessionFactory;  
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		System.out.println("-----所有Bean载入完成---");
		System.out.println("开始配置缓存");
		//解决Spring启动加载静态数据时报No Session found for current thread异常
		 Session session = sessionFactory.openSession();  
	     TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	}
}