在所有servlet(尤其是resteasy servler)初始化并且Jetty能够处理请求之后运行代码

时间:2019-08-07 15:23:40

标签: jetty guice resteasy

我们的应用程序正在Jetty中运行,并使用resteasy和guice。我需要在启动之后以及码头能够处理传入请求之后运行一些代码。

初始化resteasy servlet(类org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher)时,是否有任何监听器被调用?

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
<display-name>Example App</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<!--===========================RestEasy config==========================-->

<context-param>
    <param-name>resteasy.guice.modules</param-name>
    <param-value>com.example.MainAppInjector</param-value>
</context-param>

<listener>
    <listener-class>com.example.InitClass</listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.example.ExampleApplication</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/users/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

com.example.InitClass

public class InitClass extends GuiceResteasyBootstrapServletContextListener {

private Injector injector;

@Override
protected void withInjector(final Injector injector) {
    LOGGER.info("============ withInjector ============== ");
    super.withInjector(injector);
    // ....
}

@Override
public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);
    // ....
}

@Override
public void contextDestroyed(ServletContextEvent event) {
    super.contextDestroyed(event);
    // ....
}

}

0 个答案:

没有答案