本部分主要实现功能是:将公共的信息部分存储在Application,在程序运行时就加载。
1. Spring 监听器加载(web.xml 加 listener):
com.cocoomg.action.InitAction
2. InitAction.java 配置
@Componentpublic class InitAction implements ServletContextListener, ApplicationContextAware{ // 实现ServletContextListener 接口,实现监听 // 实现ApplicationContextAware 接口,实现对application缓存的处理 private static ApplicationContext applicationContext; //static 将 contextInitialized和setApplicationContext限制为同一个对象, //若不限制,会生成两个不同的对象,此时会报空指针异常的错误 @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub ServletContext application=servletContextEvent.getServletContext(); //获取application } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext = applicationContext; }}
3.商品大类信息加入到 application 缓存中;
-
step1:Service 接口( ProductBigTypeService.java )
public List
findAllBigTypeList(); -
step2:Service 实现( ProductBigTypeServiceImpl.java )
@Service("productBigTypeService")public class ProductBigTypeServiceImpl implements ProductBigTypeService{ @Resource private BaseDAO
baseDAO; @Override public List findAllBigTypeList() { return baseDAO.find(" from ProductBigType"); } } -
step3:InitAction 配置( InitAction.java )
@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub ServletContext application=servletContextEvent.getServletContext(); //获取application ProductBigTypeService productBigTypeService=(ProductBigTypeService)applicationContext.getBean("productBigTypeService"); List
bigTypeList= productBigTypeService.findAllBigTypeList(); application.setAttribute("bigTypeList", bigTypeList);}
4. 按照3类似将以下信息加入到 application缓存 中
- 商品小类信息加入到 application 缓存中:
- 商品大类中已经有设置 (fetch=FetchType.EAGER),表示关闭懒加载;
- 懒加载:只取出一层节点的数据,然后关闭session,这样再去取下一层级的数据的时候就会报出错误:session is closed;
- EAGER:表示取出这条数据时,它关联的数据也同时取出放入内存中;
- 即,不需要在单独将小类商品的信息加入到 application 缓存,在商品大类加入的同时商品小类就已加入。
- Tag 标签加入到 application 缓存中;
- 最新公告加入到 application 缓存中;
- 最新新闻加入到 application 缓存中;
- 今日特价加入到 application 缓存中;
- 热卖推荐加入到 application 缓存中;
5. 最终的初始化Action
@Componentpublic class InitAction implements ServletContextListener, ApplicationContextAware{ private static ApplicationContext applicationContext; @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub ServletContext application=servletContextEvent.getServletContext(); //获取application //商品大类(同时商品小类) ProductBigTypeService productBigTypeService=(ProductBigTypeService)applicationContext.getBean("productBigTypeService"); ListbigTypeList= productBigTypeService.findAllBigTypeList(); application.setAttribute("bigTypeList", bigTypeList); //标签 TagService tagService=(TagService)applicationContext.getBean("tagService"); List tagList= tagService.findTagList(); application.setAttribute("tagList", tagList); //最新公告 NoticeService noticeService=(NoticeService)applicationContext.getBean("noticeService"); List noticeList= noticeService.findNoticeList(null, new PageBean(1,7)); application.setAttribute("noticeList", noticeList); //最新新闻 NewsService newsService=(NewsService)applicationContext.getBean("newsService"); List newsList= newsService.findNewsList(null, new PageBean(1,6)); application.setAttribute("newsList", newsList); //今日特价 和 热卖推荐 ProductService productService=(ProductService)applicationContext.getBean("productService"); Product s_product=null; s_product=new Product(); s_product.setSpecialPrice(1); List specialPriceProductList= productService.findProductList(s_product, new PageBean(1,8)); application.setAttribute("specialPriceProductList", specialPriceProductList); s_product=new Product(); s_product.setHot(1); List hotProductList= productService.findProductList(s_product, new PageBean(1,6)); application.setAttribute("hotProductList", hotProductList); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext = applicationContext; }}