思路:用Java的@PostConstruct标签、@PreConstruct标签。
原理:被@PostConstruct修饰的方法会在Tomcat服务器加载Servlet的时候运行,即Tomcat服务器启动的时候运行;
被@PreConstruct修饰的方法会在Tomcat服务器卸载Servlet的时候运行,即Tomcat服务器停止的时候运行。
注意:在eclipse中停止Tomcat是不会执行被@PreConstruct修饰的方法的,必须是在Tomcat安装目录中的bin文件夹中手动关闭Tomcat服务器,才会执行被@PreConstruct修饰的方法。
参考资料:https://blog.csdn.net/jimxiaoming/article/details/51556454
https://www.cnblogs.com/landiljy/p/5764515.html