一、注解版Spring、SpringMVC配置文件
1.1、pom
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 普通maven项目中使用Sl4j注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<!-- 工具 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
</dependencies>
1.2、MySpringConfig
/**
* @Author : 一叶浮萍归大海
* @Date: 2024/1/10 14:28
* @Description: Spring的配置文件
* 功能:该配置类用于配置除了Controller的其他bean,例如:数据库连接池、事务管理器、业务bean等
* 注解说明:
* @Configuration:表示这是一个配置类,通常标识在类上,它的作用类似于xml中的 applicationContext.xml
* @ComponentScan:表示配置包扫描,里边的属性和xml配置中的属性都是一一对应的
* useDefaultFilters = true:表示使用默认的过滤器
* excludeFilters:表示去除@Controller注解标识的类,即:在 Spring 容器中扫描除了 Controller 之外的其他所有bean
*
*/
@ComponentScan(
basePackages = {"org.star"},
useDefaultFilters = true,
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)}
)
@Configuration
public class MySpringConfig {
}
1.3、MySpringMvcConfig
/**
* @Author : 一叶浮萍归大海
* @Date: 2024/1/10 14:34
* @Description: SpringMcv的配置文件
* 注解说明:
* @Configuration:表示这是一个配置类,通常标识在类上,它的作用类似于xml中的 spring-mvc.xml
* @ComponentScan:表示配置包扫描,里边的属性和xml配置中的属性都是一一对应的 useDefaultFilters = false:表示不使用默认的过滤器
* includeFilters:表示只扫描@Controller和Configuration注解标识的类,即:在 Spring 容器中只扫描 Controller 和 Configuration 标识的所有bean
* 注意:
* 如果不需要在 SpringMVC 中添加其他的额外配置,这样就可以了。即 视图解析器、JSON 解析、文件上传……等等,如果都不需要配置的话,这样就可以了
*/
@EnableWebMvc
@Configuration
@ComponentScan(
basePackages = "org.star",
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
}
)
public class MySpringMvcConfig implements WebMvcConfigurer {
@Autowired
private MyLoginHandlerInterceptor loginHandlerInterceptor;
/**
* 配置视图解析器
*
* @return
*/
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/login");
}
/**
* 自定义处理器
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 处理Controller层返回的纯字符串中文乱码
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(stringHttpMessageConverter);
// FastJson转换器(有漏洞)
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
// converters.add(fastJsonHttpMessageConverter);
// Jackson2转换器
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(jackson2HttpMessageConverter);
}
/**
* 拦截器配置
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/login","/logout");
}
}