spring boot在view层使用公共属性

2018-12-05  本文已影响0人  java修炼

spring boot需要在view层使用公共属性,在其他项目中找到的源码:

@Component("initSystem")
public class InitSystem implements ServletContextListener,ApplicationContextAware{

    private static ApplicationContext applicationContext;
    
    public static Map<Integer,ArcType> arcTypeMap=new HashMap<Integer,ArcType>();
    
    
    /**
     * 加载数据到application缓存中
     * @param application
     */
    public void loadData(ServletContext application){
        ArcTypeService arcTypeService=(ArcTypeService) applicationContext.getBean("arcTypeService");
        LinkService linkService=(LinkService) applicationContext.getBean("linkService");
        List<ArcType> arcTypeList=arcTypeService.listAll(Sort.Direction.ASC, "sort");
        application.setAttribute("allArcTypeList", arcTypeList); // 所有类别
        for(ArcType arcType:arcTypeList){
            arcTypeMap.put(arcType.getId(), arcType);
        }
        application.setAttribute("linkList", linkService.listAll(Sort.Direction.ASC,"sort")); // 所有友情链接
    }


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        loadData(sce.getServletContext());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        this.applicationContext=applicationContext;
    }

}
上一篇 下一篇

猜你喜欢

热点阅读