2012-08-23 66 views
2

我在Apache CXF的“how-to”阶段,想知道服务器启动时是否有方法调用方法。Apache CXF初始化的调用方法

这将是类似于JSF Web应用程序,当我用eager=true一个@ApplicationScoped托管bean:当容器启动时,注释的类实例化,我可以叫我无论从它的构造需要。

任何帮助?

回答

4

所以,如果你正在使用CXF Servlet服务Web Service要求,那么你就可以创建ServletContextListenercontextInitialized方法将被调用在部署或在服务器启动时,如果该应用程序已经部署。

要做到这一点创建类将实施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) {   
    } 

} 

然后在你的web.xml定义监听器:

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

contextInitialized方法,你可以通过使用获得servlet上下文:

ServletContext context = sce.getServletContext(); 

你可以设置尽可能多的属性可以在整个应用程序范围内使用。

+0

Tks,它的工作!除此之外,它是一个干净而健壮的代码。 – gfernandes