为我的 Vaadin 网络应用程序启动和停止挂钩?

本文介绍了为我的 Vaadin 网络应用程序启动和停止挂钩?的处理方法,对大家解决问题具有一定的参考价值

问题描述

我如何知道我的 Vaadin 7 Web 应用程序何时首次启动/启动,以便我可以进行一些初始设置工作?

How do I know when my Vaadin 7 web app first starting/launching, so I can do some initial set-up work?

同样,我如何知道我的网络应用何时结束、关闭/退出?

Likewise, how do I know when my web app is ending, getting shutdown/exiting?

推荐答案

ServletContextListener

Vaadin 建立在 Java Servlet 技术之上.上下文"是 Servlet 术语中 Web 应用程序的技术术语.所以这里的答案不是特定于 Vaadin 的,它适用于任何 Servlet —— 归根结底,Vaadin 只是一个大的 Servlet.

ServletContextListener

Vaadin is built on top of Java Servlet technology. A "context" is the technical term for your web app in Servlet terms. So the answer here is not Vaadin-specific, it applies to any Servlet -- and at the end of the day, Vaadin is just one big Servlet.

从 Servlet 规范 2.3 版开始,一个 Servlet 容器,例如 Tomcat, Jetty 等必须承诺留意您定义为实现 ServletContextListener 接口.该接口有两个简单的方法:

Since Servlet spec version 2.3, a Servlet container such as Tomcat, Jetty, etc. must promise to be on the lookout for any Java class you define as implementing the ServletContextListener interface. That interface has two simple methods:

结束可能是由于 Servlet 容器(例如:Tomcat)正在关闭,因此所有 Web 应用程序(上下文")都将结束,或者因为只是您的 Vaadin 应用程序的上下文正在结束(如果您的 Servlet 容器支持每个上下文关闭).

The ending could be caused by the Servlet container (ex: Tomcat) is being shutdown so all the web apps ("contexts") are ending, or because just your Vaadin app’s context is ending (if your Servlet container supports per-context shutdown).

每个 Servlet 容器必须履行的契约是你的每个 ServletContextListener 类(你可以有多个)必须在任何 servlet 之前调用它的contextInitialized或过滤器执行.因此,这是进行初始化工作的最佳时机,这可能比单个 Servlet 请求-响应周期受益更多.如果您需要启动诸如 [H2 数据库) 之类的数据库,这是个好时机.如果您将一些数据加载到内存中作为缓存供 servlet 重复使用,这是一个好时机.也是测试您的应用资源的好时机,例如确定日志记录工作或某些预期文件是否到位.

The contract every Servlet container must fulfill is that each of your ServletContextListener classes (you can have more than one) must have its contextInitialized invoked before any servlet or filter executes. So this is the perfect time to do initialization work that might benefit more than a single Servlet request-response cycle. If you need to startup a database such as [H2 Database), this is a good time. If you load some data into memory as a cache to be used by the servlet(s) repeatedly, this is a good time. Also a good time to test your apps resources, to be certain logging works or certain expected files are in place, for example.

同样,每个兼容的 Servlet 容器仅在 servlet 和过滤器完成最后一次调用之后调用contextDestroyed.因此,这是关闭数据库、进行备份或执行任何其他适合您的网络应用程序的清理工作的好地方.

Likewise, every compliant Servlet container invokes contextDestroyed only after the servlet(s) and filters have finished their last invocation. So this is a good place to shutdown your database, make backups, or do any other clean-up chore appropriate to your web app.

我们正在讨论您的网络应用上下文"的生命周期.该上下文可能涉及一个或多个 servlet.上下文的这个生命周期超出了任何一个 servlet 的生命周期参与这个上下文.背景有点像蜂王,它在一个新的蜂巢中生下了她所有的雄蜂,在它们之前她生活在那里,当它们为她尽职尽责地死去时,她将比它们活得更久(如果这就是蜂巢的方式)有用吗?).

We are discussing the life cycle of your web app’s "context". That context may involve one, or more than one, servlet. This life cycle of the context goes beyond the life cycle of any one of the servlets participating in this context. The context is kinda-sorta like the queen bee who gives birth to all her drones in a new hive, where she was living before them and she will outlive them all as they die off in dutiful service to her (if that is how a hive works?).

制作一个 ServletContextListener 非常简单:用一对方法和一个注解制作一个类.

Making a ServletContextListener is quite easy: Make a class with a pair of methods plus an annotation.

添加一个新的 Java 类作为您的 Vaadin 应用程序的一部分.您可以随意命名课程.

Add a new Java class as part of your Vaadin app. You can name the class anything you want.

我将上下文侦听器添加到与我的主要 Vaadin 应用程序相同的包中 UI 类(MyUI.java 可能由您的 Vaadin 插件或 Maven 原型生成).似乎是一个自然的地方,因为上下文侦听器是我的 Vaadin 应用程序在 any 被处理之前启动的开始,而指定的 UI 类将成为我的 Vaadin 的第一部分为每个用户运行的应用.

I add my context listeners in the same package as my main Vaadin app UI class (MyUI.java may have been generated by your Vaadin plugin or by Maven archetype). Seems like a natural place as the context listener is the beginning of my Vaadin app launching before any user is handled while the designated UI class will then be the first piece of my Vaadin app being run for each user.

将您的类声明为实现 ServleContextListener.添加上面讨论的两个必需的方法;您的 IDE 可能会协助完成这项工作.

Declare your class as implementing ServleContextListener. Add the two required methods discussed above; your IDE may assist with this chore.

还有一个技巧:您必须通知 Servlet 容器有关此上下文侦听器的信息.有不止一种方法可以做到这一点,但我使用最简单的注释 @WebListener 类.

One more trick: You must inform the Servlet container about this context listener. There is more than one way to do this, but I use the simplest, an annotation @WebListener on the class.

这是一个完整的示例类.

Here is an entire example class.

package com.example.amazingapp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 *
 * @author Basil Bourque
 */
@WebListener
public class WebAppListener implements ServletContextListener {

    @Override
    public void contextInitialized ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is starting. " );
    }

    @Override
    public void contextDestroyed ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is shutting down." );
    }

}

这篇关于为我的 Vaadin 网络应用程序启动和停止挂钩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1170

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1071

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:808

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:900

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:865

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:834

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1731

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308