SpringMVC项目中启动自加载Listener

时间:2023-03-08 16:12:11
package com.kuman.cartoon.listener;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.kuman.cartoon.entity.pointrate.PointRate;
import com.kuman.cartoon.service.pointrate.IPointRateService;
import com.kuman.cartoon.utils.PointRateUtil;

/**
 * 这个监听器用于启动的时候将积分等级信息取出,让后保存在Map集合中。
 * @author to.to
 */
@Service
public class PointRateListener implements ApplicationListener<ContextRefreshedEvent> {
	//private static final Logger LOGGER = Logger.getLogger(PointRateListener.class);

	@Autowired
	IPointRateService pointRateService;

	/**
	 * 启动加载执行
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {

		List<PointRate> pointRates = pointRateService.findAll();
		for (int i = 0; i < pointRates.size(); i++) {
			PointRateUtil.pointRateMap.put(i, pointRates.get(i));
			//LOGGER.info("PointRateUtil.pointRateMap " + i + " = " + pointRates.get(i));
		}
	}
}