在项目开发罗成中,我们往往还会碰到这样的问题:tomcate启动完成之后需要执行一段代码:那么久需要我们对tomcate的启动做监听,实现方式有阆中:一种用于普通的项目
我们可以实现servletContxtListener接口,重写其中的方法:在servelt容器加载的时候执行相应的逻辑:
如果我们是spring springMVC 的项目我们需要自己定义监听类来监听tomcate的启动:
如下:
package com.supermap.jointclient.listener;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import com.supermap.jointclient.service.ReceiveService;
import com.supermap.jointclient.util.Global;
public class ServiceListener implements ApplicationListener<ContextRefreshedEvent>{
@Autowired
ReceiveService receiveservice ;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){
String AREACODE = Global.loadProperties("config.properties").getProperty("XZQHDM");
Map map = receiveservice.getRuleInfo(AREACODE);
if(map.get("type").equals("2")){
receiveservice.excuteXMLDataToServerTask(map.get("policy").toString()) ;
}
}
}
}
在spring的配置文件中增加对应类的配置,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<bean id="springContext" class="com.supermap.base.core.SuperSpringContext"></bean>
<bean class="com.supermap.jointclient.listener.ServiceListener"/>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config.properties" />
</beans>