Call method on Apache CXF initialiaze

后端 未结 1 708
不知归路
不知归路 2021-01-22 06:48

I\'m in the \"how-to\" phase with Apache CXF and would like to know if there\'s a way to call a method when the server is started.

It would be similar to a

相关标签:
1条回答
  • 2021-01-22 07:36

    So, If you are using CXF Servlet to serve Web Service request, then you can create ServletContextListener and contextInitialized method will be called on deployment or on server start up if the application is already deployed.

    To do that create class which will implement ServletContextListener:

    public class YourContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {      
            //This method is called by the container on start up
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {        
        }   
    
    }
    

    Then define that listener in your web.xml:

    <listener>
        <listener-class>your.package.YourContextListener</listener-class>
    </listener>
    

    In the contextInitialized method you can get servlet context by using:

    ServletContext context = sce.getServletContext();
    

    And you can set as many attributes as you want to be available into whole application scope.

    0 讨论(0)
提交回复
热议问题