We know. In general, a project needs to load or run some special tasks to initialize the system when it starts. The usual practice is to initialize with a servlet. However, the servlet cannot be injected directly with the spring bean, and it needs to be configured in the Web. Xml. More trouble. Today, we introduce the method of starting initialization using spring. In fact very easy, just two steps to be able to.
- Implement the Applicationlistener interface:
publicclass Init implements ApplicationListener<ContextRefreshedEvent>{ @Override publicvoid onApplicationEvent(ContextRefreshedEvent event) { ifnull){//root application context 没有parent //TODO 这里写下将要初始化的内容 } }}
- Configuring the Bean in spring applicationcontex.xml
class="com.xx.xxx.Init"/>
This is when the service starts. will be loaded and run on their own initiative.
Web services start the use of spring to run Applicationlistener on its own initiative