springmvc配置文件换成注解替代。
springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itheima">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--放行指定类型静态资源配置方式-->
<!-- <mvc:resources mapping="/img/**" location="/img/"/>-->
<!-- <mvc:resources mapping="/js/**" location="/js/"/>-->
<!-- <mvc:resources mapping="/css/**" location="/css/"/>-->
<!--SpringMVC提供的通用资源放行方式-->
<mvc:default-servlet-handler/>
</beans>
替代的注解:要写springmvc配置类 在这个类上用注解代替
@Configuration
@ComponentScan(value = "com.itheima",includeFilters =
@ComponentScan.Filter(type=FilterType.ANNOTATION,classes = {Controller.class})
)
public class SpringMVCConfiguration implements WebMvcConfigurer{
//注解配置放行指定资源格式
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/img/**").addResourceLocations("/img/");
// registry.addResourceHandler("/js/**").addResourceLocations("/js/");
// registry.addResourceHandler("/css/**").addResourceLocations("/css/");
// }
//注解配置通用放行资源的格式
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();;
}
}
@Configuration注解 表明这是 配置类
@ComponentScan注解 用来扫描包
includeFilters 属性,用来做过滤的
@ComponentScan.Filter 用来扫描 要过滤的注解
type=FilterType.ANNOTATION 属性,说明这是过滤注解的
classes = {Controller.class}表明具体的 注解表示对那个包内进行过滤,classes要用反射的格式,就是包名的字节码文件 这是个数组类型的,所以要用大括号
代替对某些资源放行的代码
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**").addResourceLocations("/img/")}
这是对单个种类的资源进行放行的发放
对应配置文件这段内容
<mvc:resources mapping="/img/**" location="/img/"/>
注解配置通用放行资源的格式
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();;
}
对应配置文件这段内容
<!--SpringMVC提供的通用资源放行方式-->
<mvc:default-servlet-handler/>
下面是取代 web.xml 映射文件配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
创建一个配置类 继承 AbstractDispatcherServletInitializer类,
重写里面
createServletApplicationContext();
getServletMappings() ;
onStartup(ServletContext servletContext);
这三个方法
//创建Servlet容器时,使用注解的方式加载SPRINGMVC配置类中的信息,并加载成WEB专用的ApplicationContext对象
//该对象放入了ServletContext范围,后期在整个WEB容器中可以随时获取调用
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfiguration.class);
return ctx;
}
AnnotationConfigWebApplicationContext注解配置web应用文对象,与ApplicationContext对象一样的
register(SpringMVCConfiguration.class);这是将SpringMVCConfiguration配置类 注入到注解配置web应用文对象中
最后将这个对象返回
这是替代的这段话
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml.bak</param-value>
</init-param>
</servlet>
//注解配置映射地址方式,服务于SpringMVC的核心控制器DispatcherServlet
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
return new String[]{"/"}; 这句话表示过滤所有的东西
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
//乱码处理作为过滤器,在servlet容器启动时进行配置,相关内容参看Servlet零配置相关课程
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
CharacterEncodingFilter cef = new CharacterEncodingFilter();
cef.setEncoding("UTF-8");
FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter", cef);
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),false,"/*");
}
super.onStartup(servletContext);这句不能删,这是基于这个父类基础之上拓展的功能