SMM、SpringBoot 项目运行前加载数据置缓存中


前言

在项目启动前加载数据将数据存放在Application中,有效加快了服务器速度。

一、SSM项目运行前加载数据

SSM项目中需要配置一个初始化类类,实现ServletContextListener、ApplicationContextAware接口。废话不多说直接上代码

import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * @author 卢意
 * @create 2020-08-13 20:39
 */
@Component
public class InitComponent implements ServletContextListener , ApplicationContextAware {
    
    

    private static ApplicationContext applicationContext;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    
    
        ServletContext application= sce.getServletContext();//获取application对象
        BloggerService bloggerService= (BloggerService) applicationContext.getBean("bloggerService");
        Blogger blogger=bloggerService.find(); //获取博主信息
        blogger.setPassword(null);
        application.setAttribute("blogger",blogger);

        LinkService linkService= (LinkService) applicationContext.getBean("linkService");
        List<Link> linkList=linkService.linkList(null);//查询所有友情链接信息
        application.setAttribute("linkList",linkList);

        BlogTypeService blogTypeService= (BlogTypeService) applicationContext.getBean("blogTypeService");
        List<BlogType> blogTypeList=blogTypeService.countList();//查询博客类别以及博客数量
        application.setAttribute("blogTypeList",blogTypeList);

        BlogService blogService= (BlogService) applicationContext.getBean("blogService");
        List<Blog> blogCountList=blogService.countList();// 根据日期分组查询博客查询博客以及博客数量
        application.setAttribute("blogCountList",blogCountList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    
    

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        //获取application
        this.applicationContext=applicationContext;
    }
}

配置完之后 还需需要在web.xml写上

  <!--网站初始化监听-->
  <listener>
    <listener-class>com.luyi.service.impl.InitComponent</listener-class>
  </listener>

这样就完成了ssm启动项目加载数据到Applocation中
当然 后台实现数据更新之后还需要重新加载数据 这个可以直接写在Controller层 需要的时候调用即可。代码如下

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统管理controller层
 * @author 卢意
 * @create 2020-08-21 18:47
 */
@Controller
@RequestMapping("/admin/system")
public class SystemAdminController {
    
    
	@Resource
	private BlogService blogService;
	@Resource
	private LinkService linkService;
	@Resource
	private BlogTypeService blogTypeService;
	@Resource
	private BloggerService bloggerService;

	/**
	 * 刷新系统缓存
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/refreshSystem")
	public String refreshSystem(HttpServletRequest request, HttpServletResponse response)throws Exception{
    
    
		ServletContext application=RequestContextUtils.findWebApplicationContext(request).getServletContext();
		Blogger blogger=bloggerService.find(); //获取博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger",blogger);

		List<Link> linkList=linkService.linkList(null);//查询所有友情链接信息
		application.setAttribute("linkList",linkList);

		List<BlogType> blogTypeList=blogTypeService.countList();//查询博客类别以及博客数量
		application.setAttribute("blogTypeList",blogTypeList);

		List<Blog> blogCountList=blogService.countList();// 根据日期分组查询博客查询博客以及博客数量
		application.setAttribute("blogCountList",blogCountList);
		JSONObject result=new JSONObject();
		result.put("success",true);
		ResponseUtil.write(response,result);
		return null;
	}
}

这样算是全套配套完成了

二、Spring Boot 项目运行前加载数据

1.重写方法

Spring Boot真的很不错,把这个功能封装的很好,不需要像SSM项目那么麻烦。
实现 CommandLineRunner和ServletContextListener接口

import com.luyi.entity.Carousel;
import com.luyi.entity.FlowerType;
import com.luyi.entity.Flowers;
import com.luyi.service.CarouselService;
import com.luyi.service.FlowerTypeService;
import com.luyi.service.FlowersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * 项目启动时  执行器
 * @author 卢意
 * @create 2020-09-19 10:36
 */
@Component("startUpRunner")
public class StarUpRunner implements CommandLineRunner, ServletContextListener {
    
    

	private ServletContext application=null;

	@Autowired
	private CarouselService carouselService;

	@Autowired
	private FlowerTypeService flowerTypeService;

	@Autowired
	private FlowersService flowersService;




	@Override
	public void run(String... args) throws Exception {
    
    
		this.loadData();
	}

	public void loadData(){
    
    
			List<Carousel> carouselList=carouselService.list();
			application.setAttribute("carouselList",carouselList);
			List<FlowerType> flowerTypeIndexList=flowerTypeService.getIsShowFlowerType();
			for (FlowerType flowerType:flowerTypeIndexList ){
    
    
				List<Flowers> flowersList=flowersService.getFlowersByTypeId(flowerType.getId());
				flowerType.setFlowersList(flowersList);
			}
			application.setAttribute("flowerTypeIndexList",flowerTypeIndexList);
	}

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

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
    
    

	}
}

``

2.数据变更

.数据变更时直接调用该类即可

				carouselService.update(carousel);
				starUpRunner.loadData();

总结

刚开始写博客,目的是为了与大家分享一下采坑经验和为了整理材料以后遇到问题就不用了百度了看看自己的博客,也可以温故而知新。不喜勿喷。也欢迎各位大佬提提学习意见。

猜你喜欢

转载自blog.csdn.net/weixin_43691773/article/details/108745396