文章目录
spring mvc纯注解配置
参考
Spring MVC4 纯注解配置教程 https://www.cnblogs.com/csonezp/p/5315725.html
spring和springmvc纯注解整合 https://blog.csdn.net/sssyyyy7/article/details/90613162
1.环境
maven
spring5.2.6
2.maven依赖
依赖的jar
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
使用的 idea中tomcat 或 maven jetty插件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
tomcat的在idea中乱码问题
设置tocmat插件的 VM参数
2.配置
2.1启动配置
方式一 推荐
web项目中启动
public class AnnotationConfigDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* rootConfig
* createRootApplicationContext() root application context
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
/**
* web 应用填写这个配置就可以
* createServletApplicationContext() Servlet application context
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
// return new Class[0];
return new Class<?>[]{RootConfig.class};
}
/** 配置spring mapping访问 */
@Override
protected String[] getServletMappings() {
// return new String[0];
// return new String[]{"/"};
return new String[]{"*.do"};
}
/**
* 注册 filter
*/
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[]{characterEncodingFilter};
}
}
方式二
实现 WebApplicationInitializer 接口 MyWebAppInitializer
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
ServletContext container = servletContext;
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
2.2 spring rootConfig配置
RootConfig
@EnableWebMvc
@Configuration
@ComponentScan(basePackages={"com.lwp.**.controller","com.lwp"})
public class RootConfig {
}
2.3 springmvc配置
SpringMvcConfig
SpringMvcConfig
继承 WebMvcConfigurationSupport 实现设置配置
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {
/**
* 配置springmvc视图
* @return
*/
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
// resolver.setPrefix("/pages/");
// resolver.setPrefix("/WEB-INF/pages/");
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
// 文件上传
/* public CommonsMultipartResolver commonsMultipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
// 默认 -1 indicates no limit
// 100M = 104857600
// commonsMultipartResolver.setMaxUploadSize(104_857_600);
retrn commonsMultipartResolver;
}*/
@Bean
public StringHttpMessageConverter stringHttpMessageConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
return stringHttpMessageConverter;
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
/*FastJsonHttpMessageConverter fjc = new FastJsonHttpMessageConverter();
FastJsonConfig fj = new FastJsonConfig();
fj.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
fjc.setFastJsonConfig(fj);
converters.add(fjc);*/
// 新增自定义消息转换
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
// MediaType.valueOf("text/plain;charset=UTF-8")
// MediaType.valueOf("text/html;charset=UTF-8")
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
converters.set(1, stringHttpMessageConverter);
}
}
自定义消息转换器
默认的转换器
其他
执行顺序由先到后
- WebApplicationInitializer
- ApplicationContextAware
1. WebApplicationInitializer
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("WebApplicationInitializer ...");
/*ServletContext container = servletContext;
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");*/
}
}
2. ApplicationContextAware
@Component
public class MyWebApplicationAware implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(applicationContext);
System.out.println(99999999);
}
}