Error creating beans dispatcherServlet & dispatcherRegistration after upgrading to Spring bootVersion 5.3.18

Viewed 151

I am getting the following error in the logs while deploying my application on websphere 9.0 Here is the full stacktrace:

[4/28/22 1:34:34:811 PKT] 000000fc SystemErr R Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatcherRegistration' defined in class path resource [com/tlx/configurations/WebAppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'dispatcherRegistration' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatcherServlet' defined in class path resource [com/tlx/configurations/WebAppConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.tlx.configurations.LoggableDispatcherServlet] from ClassLoader [ com.ibm.ws.classloader.CompoundClassLoader@f3ffa9d0[war:tlxfs_war/tlxfs.war]

[4/28/22 1:34:34:811 PKT] 000000fc SystemErr R Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatcherRegistration' defined in class path resource [com/tlx/configurations/WebAppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'dispatcherRegistration' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatcherServlet' defined in class path resource [com/tlx/configurations/WebAppConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.tlx.configurations.LoggableDispatcherServlet] from ClassLoader [ com.ibm.ws.classloader.CompoundClassLoader@f3ffa9d0[war:tlxfs_war/tlxfs.war]

The two functions causing error are

  1. public DispatcherServlet dispatcherServlet()
  2. public ServletRegistrationBean dispatcherRegistration()

I am able to comment the two functions and deployed successfully, but my question is what is the error with them after upgradation however they were working fine before. The error comes only in deployment, the code runs perfectly fine.

Here is the complete file:

package com.tlx.configurations;

import java.util.TimeZone;

import javax.annotation.PostConstruct;
import javax.servlet.MultipartConfigElement;

import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.tlx.configurations.filters.PersistAuthTokenFilter;
import com.tlx.core.repositories.AclUserRepository;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableAutoConfiguration(exclude = { WebMvcAutoConfiguration.class })
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@EnableSwagger2
// @EnableCaching
@EnableAsync
@EnableWebMvc
public class WebAppConfig implements WebMvcConfigurer {

    // public class WebAppConfig extends WebMvcConfigurerAdapter{
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public ServletRegistrationBean<DispatcherServlet> dispatcherRegistration() {
        ServletRegistrationBean<DispatcherServlet> registerationBean = new ServletRegistrationBean<DispatcherServlet>(dispatcherServlet());
        registerationBean.setMultipartConfig(new MultipartConfigElement(""));
        return registerationBean;
    }

    @Bean
    AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }

    @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet() {
        return new LoggableDispatcherServlet();
    }

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper mm = new ModelMapper();

        mm.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);
        mm.getConfiguration().setAmbiguityIgnored(true);
        return mm;
    }

    @PostConstruct
    void started() {
        TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
    }

    @Bean
    public ObjectWriter objectWriter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
        return mapper.writer().withDefaultPrettyPrinter();
    }

    @Bean
    public NonContextualLobCreator getNonContextualLobCreator() {
        return NonContextualLobCreator.INSTANCE;
    }

    @Bean
    public FilterRegistrationBean loggingFilter(AclUserRepository repo) {

        FilterRegistrationBean registrationBean = new FilterRegistrationBean();

        registrationBean.setFilter(new PersistAuthTokenFilter(repo));
        // Add repo here;
        registrationBean.addUrlPatterns("/oauth/*");
        return registrationBean;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipart = new CommonsMultipartResolver();
        multipart.setMaxUploadSize(3 * 1024 * 1024);
        return multipart;
    }

    @Bean
    @Order(0)
    public MultipartFilter multipartFilter() {
        MultipartFilter multipartFilter = new MultipartFilter();
        multipartFilter.setMultipartResolverBeanName("multipartReso‌​lver");
        return multipartFilter;
    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        // tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    @Bean(name = "filterMultipartResolver")
    public CommonsMultipartResolver commonsMultipartResolver() {
        final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(-1);
        return commonsMultipartResolver;
    }
    
    @Bean
    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();
        vr.setPrefix("/WEB-INF/jsps/");
        vr.setSuffix(".jsp");
        return vr;
    }
    
    @Bean 
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    } 
    
}
0 Answers
Related