猿猴:对于Java web项目启动时执行某一操作的理解

1、Spring 容器初始化完成之后执行

实现

(1)首先自定义Java类,实现接口ApplicationListener<ContextRefreshedEvent> 

/**
 * Spring容器都初始化完成之后做操作
 * 
 * @author 猿猴
 *
 */
public class ContextFileListener implements ApplicationListener<ContextRefreshedEvent> {
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		 
		//这里写你要操作的代码
         //.......................
		 
	}

}

(2)在Spring的配置文件applicationContext.xml中配置

	<!-- Spring容器都初始化完成之后做操作 -->
	<bean id="contextFileListener" class="com.yuanhou.listener.ContextFileListener">
	</bean>

以上两步操作完成即可,Java web项目在启动时候,等到Spring容器全部初始化完成以后就会去自动执行自定义类ContextFileListener中的代码。

猜你喜欢

转载自blog.csdn.net/xinchunge12/article/details/81808104
今日推荐