创建名称为'defaultServletHandlerMapping的bean时出错

编程入门 行业动态 更新时间:2024-10-26 10:33:41
本文介绍了创建名称为'defaultServletHandlerMapping的bean时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个eclipse上的spring(具有spring安全性)+ java + maven应用程序.提交注册表单时遇到以下错误.随后查看我的其余文件:

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

我的文件:

AppInit

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

MyApp配置文件:

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文件:

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() }; } }

安全配置文件:

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!

推荐答案

我在此平台上针对类似问题的解决方案的帮助下解决了该问题.

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

我通过将以下注释放入myappconfig文件中来排除配置过滤器的扫描:

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.

然后我删除了@EnableWebMvc并将其放入myappinit文件中,问题已解决.

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

我的猜测是@EnableWebMvc和@ComponentScan(basePackages ={ "com.myapp" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })不应位于同一配置文件中.

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.

更多推荐

创建名称为'defaultServletHandlerMapping的bean时出错

本文发布于:2023-10-28 17:50:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:defaultServletHandlerMapping   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!