【问题标题】:How to use listener-class in spring boot 2.2.1 RELEASEspring boot 2.2.1 RELEASE中如何使用listener-class
【发布时间】:2019-11-22 03:50:43
【问题描述】:

将 Spring Web 项目迁移到具有以下版本的 Spring Boot 应用程序中

Spring version:-4.1.7
Spring boot:2.2.1 RELEASE

迁移的一部分我需要删除 src/main/web-app 文件夹。在这个文件夹中我有两个文件

dispatcher-servlet.xml
web.xml

调度程序-servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.data.services" />
    <bean id="systemPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

    <mvc:interceptors>
        <bean class="com.data.services.interceptors.RequestLogInterceptor" />
    </mvc:interceptors>
</beans>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="true" version="3.0"> 
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
         <load-on-startup>1</load-on-startup>
         <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
     </servlet-mapping>

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>XERService</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/conf/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>30000</param-value>
    </context-param> 



    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>xerwebapp.root</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.data.services.configuration.ServiceContextListener</listener-class>
    </listener>

</web-app>

ServiceContextListener

@WebListener("Service context listener")
public class ServiceContextListener implements ServletContextListener
{
    public static final Logger LOG = Logger.getLogger("XERService");

    /**
     * Context initializer. This method sets the root folder for the log4j
     */
    @Override
    public void contextInitialized(ServletContextEvent event)
    {
    }

    @Override
    public void contextDestroyed(ServletContextEvent event)
    {
        try
        {
            LoggingPropertyHandler.clear();
            if (XFabSiteConfigManager.getInstance().getRequesterConfigInfo() != null)
            {
                XFabFactory.getXfabInstance().unInitializeRequester();
            }
        }
        catch (Exception exception)
        {
            LOG.warn("Exception while uninitializing Requestor", exception);
        }
    }
}

RequestLogInterceptor

@Component
public class equestLogInterceptor extends HandlerInterceptorAdapter
{
    /**
     * Method to add the Audit user in the log
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        String auditUser = request.getHeader(RequestParameterConstants.AUDIT_USER);
        if (auditUser == null)
        {
            auditUser = "";
        }
        LoggingPropertyHandler.addAuditUserName(auditUser);
        return true;
    }
}

我们如何在 Spring Boot 中包含以下监听器

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.data.services.configuration.ServiceContextListener</listener-class>
    </listener> 

spring boot 中有没有最简单的机制。

【问题讨论】:

标签: spring spring-boot spring-mvc maven-3


【解决方案1】:

您可以像这样创建 Listener bean:

    // Register ServletContextListener
    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> listenerRegistrationBean() {
        ServletListenerRegistrationBean<ServletContextListener> bean = 
            new ServletListenerRegistrationBean<>();
        bean.setListener(new MyServletContextListener());
        return bean;
    }

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 2020-07-11
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2016-06-26
    • 2018-10-22
    相关资源
    最近更新 更多