springMVC通过实现ServletContextAware接口,来实现初始化后运行想要启动的类。
@Component
public class CommonStart implements ServletContextAware {
@Autowired
Service service;
@Override
public void setServletContext(ServletContext servletContext) {
long start = System.currentTimeMillis();
ExecutorService service = Executors.newCachedThreadPool();
System.out.print(System.currentTimeMillis()-start);
service.execute(new logReceive());
}
public class logReceive implements Runnable{
@Override
public void run() {
//TODO
}
}
}
springboot通过实现CommandLineRunner接口,来实现初始化后运行想要启动的类。
@Component
public class UdpServerBootstrap implements CommandLineRunner {
@Autowired
private PacketRev packetRev;
@Override
public void run(String... args) throws Exception {
Thread thread = new Thread(packetRev);
thread.start();
}
}
还有就是在xml中定义bean,项目启动就运行其init方法来实现。