ServletContextLister를 구현한 웹 애플리케이션 시작 시 작업

■ 참고 : http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/
 

■ ServletContextLister를 구현하면 웹 애플리케이션이 시작된 직후에 필요한 작업을 할 수 있다.

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.beans.factory.annotation.Autowired;

public class StartupListener implements ServletContextListener {
       
        /**
        * 웹 애플리케이션 시작
        */
        @Autowired
        public void contextInitialized(ServletContextEvent event) {              
              System. out.println( "StartupListener..." );
       }

        /**
        * 웹 애플리케이션 종료
        */
        public void contextDestroyed(ServletContextEvent arg0) {}
}

 

■ StartupListener를 web.xml에 등록한다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
...
    <listener>
        <listener-class>net.iotinfra.pilot.listener.StartupListener</listener-class>
    </listener>
...
</web-app>