spring 设置全局list变量,保存数据库只取一次的数据字典

@Component
public class ProgramId {

	private static ProgramIdDao dao;

	private static volatile List programIdList = null;

	/**
	 *
	 * @param dao 
	 */
	@Autowired
	public ProgramId(ProgramIdDao dao) {
		ProgramId.dao = dao;
	}
	/**
	 *
	 * @param methodName
	 * @return 
	 */
	public static ProgramIdWithMethodName getProgramId(String methodName) {
		if (programIdList == null) {
			refreshProgramIdList();
		}

		for (ProgramIdWithMethodName info : programIdList) {
			if (info.getMethodName().equals(methodName)) {
				return info;
			}
		}
		return null;
	}

	/**
	 * 
	 */
	public static void refreshProgramIdList() {
		synchronized (ProgramId.class) {
			programIdList= dao.getProgramIdList().stream()
					.map(ProgramIdListEntity::ProgramIdWithMethodName)
					.collect(Collectors.toList());
		}
    }
}

你可能感兴趣的:(spring)