Error creating bean with name 'defaultServletHandlerMapping'
来源:13-7 web层的迁移_
lawson_gjs
2020-12-28
老师你好,
MvcConfiguration配置中
/**
* 定义默认的请求处理器
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
这个配置出错,去掉这个就没异常出现,很焦虑,望老师帮忙看一下,谢谢。
这是代码
package com.imooc.o2o.config.web;
import javax.servlet.ServletException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.google.code.kaptcha.servlet.KaptchaServlet;
/**
* 开启Mvc,自动注入spring容器。 WebMvcConfigurerAdapter:配置视图解析器
* 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean
*
* @author xiangze
*
*/
@Configuration
// 等价于<mvc:annotation-driven/>
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware {
// Spring容器
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 静态资源配置
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
// registry.addResourceHandler("/upload/**").addResourceLocations("file:/Users/baidu/work/image/upload/");
}
/**
* 定义默认的请求处理器
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* 创建viewResolver
*
* @return
*/
@Bean(name = "viewResolver")
public ViewResolver createViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// 设置Spring 容器
viewResolver.setApplicationContext(this.applicationContext);
// 取消缓存
viewResolver.setCache(false);
// 设置解析的前缀
viewResolver.setPrefix("/WEB-INF/html/");
// 设置试图解析的后缀
viewResolver.setSuffix(".html");
return viewResolver;
}
/**
* 文件上传解析器
*
* @return
*/
@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setDefaultEncoding("utf-8");
// 1024 * 1024 * 20 = 20M
multipartResolver.setMaxUploadSize(20971520);
multipartResolver.setMaxInMemorySize(20971520);
return multipartResolver;
}
}
写回答
3回答
-
qq_柠檬脾气_3
2021-03-21
请问同学是怎么解决这个问题的
012022-01-29 -
qq_慕九州3211669
2021-01-07
遇到同样的问题,目前https://start.spring.io这个网址上spring boot 的最低版本是2.3.7,和视频上的1.5.8 对应不上,不知该如何处理
012021-01-07 -
翔仔
2020-12-29
同学好,请问版本一致么,如果不一致可能就会报这个错误,请确保jdk 还有springboot版本一致,后续会统一带着升级
00
相似问题