gpt4 book ai didi

java - Apache CXF initialiaze 上的调用方法

转载 作者:行者123 更新时间:2023-11-30 07:23:18 24 4
gpt4 key购买 nike

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

它类似于 JSF web 应用程序,当我使用 @ApplicationScoped 托管 bean 和 eager=true 时:当容器启动后,带注释的类被实例化,我可以从它的构造函数中调用任何我想要的东西。

有什么帮助吗?

最佳答案

因此,如果您正在使用 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();

并且您可以设置任意数量的属性以在整个应用程序范围内可用。

关于java - Apache CXF initialiaze 上的调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com