springMVC全注解开发
消除springmvc.xml
创建配置类
@Configuration
@ComponentScan({"com.java.service", "com.java.web.controller"})
@EnableWebMvc//<mvc:annotation-driven/> <mvc:default-servlet-handler/>
public class MyConfigClass {
/**
* 文件上传解析器 id固定为multipartResolver
* <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
*
* @return
*/
@Bean("multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("UTF-8");
commonsMultipartResolver.setMaxUploadSize(102400);
return commonsMultipartResolver;
}
}
定义一个类继承AnnotationConfigWebApplicationContext,在自定义类的构造里注册核心配置类:
public class MyAnnotationApplicationContext extends AnnotationConfigWebApplicationContext {
public MyAnnotationApplicationContext() {
//注册配置类
super.register(MyConfigClass.class);
}
}
在web.xml配置前端处理器的位置初始化配置类:
<init-param>
<!--加载配置类-->
<param-name>contextClass</param-name>
<param-value>com.java.config.MyAnnotationApplicationContext</param-value>
</init-param>
消除web.xml
创建自定义类继承抽象类AbstractAnnotationConfigDispatcherServletInitializer,并且实现里面的抽象方法:
public class MyAbstractAnnotationConfigDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
//加载spring的配置类
protected Class<?>[] getRootConfigClasses() {
return new Class[]{MySpringConfigClass.class};
}
@Override
//加载springmvc配置类
protected Class<?>[] getServletConfigClasses() {
return new Class[]{MySpringMvcConfigClass.class};
}
@Override
//加载前端处理器的映射路径
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
删除web.xml,发现运行正常,消除成功!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?