javaEE EAR-项目类似 onload 或启动方法

发布于 2024-12-21 06:40:51 字数 357 浏览 2 评论 0原文

可能的重复:
Java EE 企业应用程序:在部署/启动时执行某些操作

有没有一种方法可以为 javaEE Ear 服务器应用程序定义类似加载方法之类的内容。

例如,我在 JBoss EAR 服务器上使用 Hibernate,该服务器在整个应用程序生命周期中需要一个 SessionFactory 实例。

Possible Duplicate:
Java EE Enterprise Application: perform some action on deploy/startup

Is there a way to define something like a on load method for a javaEE ear server application.

For example I use Hibernate on a JBoss EAR Server which needs one SessionFactory instance for the whole application lifetime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2024-12-28 06:40:51

使用 ServletContextListener

@WebListener
public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Do stuff during webapp's startup.
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Do stuff during webapp's shutdown.
    }

}

过滤器 (如果您有计划的话特别有用实现“视图中打开会话”模式)

@WebFilter("*.xhtml") // Or whatever URL pattern
public class OpenSessionInViewFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // Do stuff during filter's init (so, during webapp's startup).
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // Do stuff during every request on *.xhtml (or whatever URL pattern)
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // Do stuff during filter's destroy (so, during webapp's shutdown).
    }

}

Use either a ServletContextListener

@WebListener
public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Do stuff during webapp's startup.
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Do stuff during webapp's shutdown.
    }

}

or a Filter (particularly useful if you plan to implement "open session in view" pattern)

@WebFilter("*.xhtml") // Or whatever URL pattern
public class OpenSessionInViewFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // Do stuff during filter's init (so, during webapp's startup).
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // Do stuff during every request on *.xhtml (or whatever URL pattern)
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // Do stuff during filter's destroy (so, during webapp's shutdown).
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文