让tomcat启动后自动进行一些操作
需求:在Tomcat启动后,自动调用一个WebService接口,得到数据,插入本机的数据库,即同步数据。本身的程序也是WebService,要求不需要外部触发,启动时就同步。
Web.xml:
<listener>
<listener-class>org.onlineservice.listener.StartupListener</listener-class>
</listener>
<listener-class>org.onlineservice.listener.StartupListener</listener-class>
</listener>
listener
package org.onlineservice.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
public class StartupListener extends ContextLoaderListener implements
ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent event)
{
//要执行的代码放在这里
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
// TODO Auto-generated method stub
super.contextDestroyed(event);
}
}
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
public class StartupListener extends ContextLoaderListener implements
ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent event)
{
//要执行的代码放在这里
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
// TODO Auto-generated method stub
super.contextDestroyed(event);
}
}