-1

Migrating Spring web project into spring boot application with following versions

Spring version:-4.1.7
Spring boot:2.2.1 RELEASE

part of migration i need to remove src/main/web-app folder.In this folder i have two files

dispatcher-servlet.xml
web.xml

dispatcher-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;
    }
}

How can we include following listener in spring boot

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

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

Is there any simplest mechanism available in spring boot.

arj
  • 887
  • 1
  • 15
  • 37

1 Answers1

0

You can create the Listener bean like this:

    // Register ServletContextListener
    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> listenerRegistrationBean() {
        ServletListenerRegistrationBean<ServletContextListener> bean = 
            new ServletListenerRegistrationBean<>();
        bean.setListener(new MyServletContextListener());
        return bean;
    }
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Abid Khan
  • 11
  • 3