Error creating bean with name 'defaultServletHandlerMapping

20,067

I solved the problem with help from a solution to a similar problem on this Platform.

I excluded configuration filters from being scanned by putting the following annotations in the myappconfig file:

@EnableWebMvc 
@Configuration
@ComponentScan(
  basePackages ={ "com.myapp" }, 
  excludeFilters = { 
    @Filter(type = FilterType.ANNOTATION, value = Configuration.class)
  }
)

After doing this, the problem was not solved.

Then I removed @EnableWebMvc and put in myappinit file and issue was solved.

My guess is that @EnableWebMvc and @ComponentScan(basePackages ={ "com.myapp" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) }) shouldn't be in the same config file.

Share:
20,067
Edwardo Afundoh
Author by

Edwardo Afundoh

Updated on January 12, 2020

Comments

  • Edwardo Afundoh
    Edwardo Afundoh over 4 years

    This is a spring (with spring security) + java + maven application on eclipse. I encounter the following error when Submit a signup form. See the rest of my files subsequently:

    HTTP Status 500 - Request processing failed; nested exception is 
    org.springframework.beans.factory.BeanCreationException:
     Error creating bean with name 'defaultServletHandlerMapping' defined in class 
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: 
    Bean instantiation via factory method failed; nested exception is 
    org.springframework.beans.BeanInstantiationException: Failed to instantiate 
    [org.springframework.web.servlet.HandlerMapping]:
     Factory method 'defaultServletHandlerMapping'
     threw exception; nested exception is java.lang.IllegalArgumentException:
     A ServletContext is required to configure default servlet handling
    

    My files:

    AppInit

    package com.myapp.config;
    
    import org.springframework.security.web.context.*;
    
    public class AppInit       extends AbstractSecurityWebApplicationInitializer                    
    
    }
    

    MyApp config file:

    package com.myapp.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
    
    import com.myapp.JDBC.EmailJDBC;
    import com.myapp.JDBC.LastIdJDBC;
    import com.myapp.JDBC.LoginJDBC;
    import com.myapp.JDBC.PersonJDBC;
    
    @EnableWebMvc //mvc:annotation-driven
    @Configuration
    
    @ComponentScan(basePackages ={ "com.myapp" })//, excludeFilters = { 
    //@Filter(type = FilterType.ANNOTATION, value = Configuration.class) })
    
    public class myappConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        }
    
        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass(JstlView.class);
            viewResolver.setPrefix("/WEB-INF/views/jsp/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
    
        @Bean
        public DriverManagerDataSource getDatasource(){
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setPassword("1234567");
            dataSource.setUrl("jdbc:mysql://localhost:3306/myapp");
            dataSource.setUsername("root");
            return dataSource;
        }
        @Bean
        public LoginJDBC getLoginBean(){
            LoginJDBC bean = new LoginJDBC();
            bean.setDataSource(new myappConfig().getDatasource());
            return bean;
        }
        @Bean
        public PersonJDBC getPersonBean(){
            PersonJDBC bean = new PersonJDBC();
            bean.setDataSource(new myappConfig().getDatasource());
            return bean;
        }
        @Bean
        public EmailJDBC getEmailBean(){
            EmailJDBC bean = new EmailJDBC();
            bean.setDataSource(new myappConfig().getDatasource());
            return bean;
        }
        @Bean
        public LastIdJDBC getLastIdBean(){
            LastIdJDBC bean = new LastIdJDBC();
            bean.setDataSource(new myappConfig().getDatasource());
            return bean;
        }
    }
    

    WebInit file:

    package com.myapp.config;
    
    import javax.servlet.Filter;
    
    import org.springframework.core.annotation.Order;
    import org.springframework.web.filter.HiddenHttpMethodFilter;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    
    public class myappWebInit extends
            AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] { myappConfig.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] { "/","/login", "/signuPerson","/regPerson","/regPersonSuccess" };
        }
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { RootConfiguration.class };
        }
        @Override
        protected Filter[] getServletFilters() {
            return new Filter[] { new HiddenHttpMethodFilter() };
        }
    }
    

    Security config file:

    package com.myapp.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.*;
    import org.springframework.security.web.csrf.CsrfTokenRepository;
    import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
    
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled=true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
        @Autowired
        private CsrfTokenRepository csrfTokenRepository() 
        { 
            HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
            repository.setSessionAttributeName("_csrf");
            return repository; 
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
        http
            .csrf()
                .csrfTokenRepository(csrfTokenRepository())
                .and()
            .authorizeRequests()
                .antMatchers("/resources/**", "/signuPerson").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                //.failureUrl(authenticationFailureUrl)
                .permitAll()
                .and()
            .logout()
                .permitAll();
    
    
        }
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");
        }
    }
    

    I apologize for putting a lot of stuff. I already tried a solution to a problem similar to this but it didn't workout. Any hint would be much appreciated. Thanks in advance!

    • Shinde Arjun
      Shinde Arjun over 8 years
      Try removing the file AppInit for a sec and rebuild your app! tell me if it helps!!
    • Edwardo Afundoh
      Edwardo Afundoh over 8 years
      @Mr.Arjun I tried but problem wasn't solved. I guess the problem is at the level of the signup controller. Somehow the request handler is not well defined. But I don't know how to go about it. In addition, I could not find the 'defaultHandlerBean' that the program tried to create and instantiate.Thanks!