基于Spring MVC的web应用随应用启动而加载
写个类实现org.springframework.context.ApplicationContextAware接口即可。
但是如下的程序会在启动时加载两次:
@Controller public class MyAction implements ApplicationContextAware { @Autowired private MyBo myBo; private Logger LOG = Logger.getLogger(MyAction.class); class Thread1 implements Runnable{ public void run() { myBo.myMethod(); } } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { LOG.info("启动线程1..."); new Thread(new Thread1()).start(); } }
控制台输出了两次“启动线程1...”
解决办法是去掉class前面的@Controller注解。