gfernandes
gfernandes

Reputation: 57

Call method on Apache CXF initialiaze

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 JSF web application, when I use a @ApplicationScoped managed bean with eager=true: when the container is started, the annotated class is instantiated and I can call whatever I want from its constructor.

Any help?

Upvotes: 2

Views: 1199

Answers (1)

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

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.

Upvotes: 4

Related Questions