@Component public class InitDataCache { public static List<String> list = new ArrayList<String>(); //注解是操作的关键 @PostConstruct public void init(){ System.out.println("系统运行开始"); //放数据到静态的list中就是放到内存中,全局通用 } @PreDestroy public void destroy(){ System.out.println("系统运行结束"); } }
@Component:声明该类交给Spring管理,是一个Bean
@PostConstruct:程序运行时会先执行该方法相当于servlet的init
@ProDestroy : 程序结束运行前执行该方法相当于servlet的destroy