索引:
参看代码 GitHub:
一、引入必要类库
spring-context
spring-context-support
spring-webmvc:引入该包后,maven 会自动解析依赖,引入 spring-web 等包。
1.solution/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>lm.solution</groupId>
<artifactId>solution</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>webapi</module>
<module>web</module>
<module>common</module>
<module>service</module>
<module>mapper</module>
<module>pojo</module>
<module>console</module>
<module>webservice</module>
</modules>
<packaging>pom</packaging> <name>solution</name>
<url>http://maven.apache.org</url> <properties>
<!--编译字符集-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring -->
<spring.version>4.3.13.RELEASE</spring.version>
<!-- log4j -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<!-- jackson -->
<jackson.version>2.9.4</jackson.version>
<!-- junit -->
<junit.version>4.12</junit.version>
<!-- aspectj -->
<aspectj.version>1.8.13</aspectj.version>
<!-- cglib -->
<cglib.version>3.1</cglib.version>
<!-- mybatis -->
<mybatis.version>3.4.5</mybatis.version>
<!-- mybatis-spring -->
<mybatisSpring.version>1.3.1</mybatisSpring.version>
<!--mysql-connector-->
<mysql.version>5.1.34</mysql.version>
<!--druid-->
<druid.version>1.0.5</druid.version>
<!--javax.servlet-->
<javaxServlet.version>3.0.1</javaxServlet.version>
<!--jsp-api-->
<jspApi.version>2.2</jspApi.version>
<!--jstl-->
<jstl.version>1.2</jstl.version>
<!--json-lib-->
<jsonLib.version>2.1</jsonLib.version>
<!--jackson old-->
<jacksonOld.version>1.9.13</jacksonOld.version>
<!--dom4j-->
<dom4j.version>1.6.1</dom4j.version>
<!--ehcache core-->
<ehcacheCore.version>2.6.9</ehcacheCore.version>
<!--ehcache web-->
<ehcacheWeb.version>2.0.4</ehcacheWeb.version>
<!--commons-fileupload-->
<commonsFileupload.version>1.3.1</commonsFileupload.version>
<!--commons-io-->
<commonsIo.version>2.4</commonsIo.version>
<!--commons-codec-->
<commonsCodec.version>1.9</commonsCodec.version>
<!--commons-collections4-->
<commonsCollections4.version>4.0</commonsCollections4.version>
<!--commons-beanutils-->
<commonsBeanutils.version>1.7.0</commonsBeanutils.version>
<!--freemarker-->
<freemarker.version>2.3.19</freemarker.version>
<!-- poi poi-ooxml -->
<poi.version>3.9</poi.version>
<!--org.apache.httpcomponents-->
<httpcore.version>4.4.8</httpcore.version>
<httpclient.version>4.5.4</httpclient.version>
<!-- jaxws-rt -->
<jaxwsRt.version>2.3.0</jaxwsRt.version>
<!-- jedis -->
<jedis.version>2.9.0</jedis.version>
<!-- rabbitmq -->
<amqpClient.version>5.1.2</amqpClient.version>
<!--fastjson-->
<fastjson.version>1.2.46</fastjson.version>
<!-- jsr250 -->
<jsr250.version>1.0</jsr250.version>
</properties> <dependencies>
<!--单元测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<!--test 说明这个包的存活是在test周期,也就是发布时将不包含这个jar包-->
<scope>test</scope>
</dependency>
<!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<!-- 因依赖 会自动引入 spring-aop spring-beans spring-core spring-expression 四个包 -->
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aspectj AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- 映入JSON jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<!--spring web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--cglib-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatisSpring.version}</version>
</dependency>
<!-- Mysql数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
<!--<scope>runtime</scope>-->
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javaxServlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jspApi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- 映入JSON lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${jsonLib.version}</version>
<classifier>jdk15</classifier>
</dependency>
<!-- jackson old -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jacksonOld.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jacksonOld.version}</version>
</dependency>
<!-- 用dom4j解析xml文件 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcacheCore.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
<version>${ehcacheWeb.version}</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commonsFileupload.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commonsIo.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commonsCodec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commonsCollections4.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!-- commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commonsBeanutils.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- org.apache.httpcomponents -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- com.sun.xml.ws/jaxws-rt -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxwsRt.version}</version>
</dependency>
<!-- redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>${amqpClient.version}</version>
</dependency>
<!-- com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- javax.annotation/jsr250-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>${jsr250.version}</version>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins> <pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>3.0-r1756463</version>
</plugin>
</plugins>
</pluginManagement>
</build> </project>
2.web/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>solution</artifactId>
<groupId>lm.solution</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>lm.solution</groupId>
<artifactId>web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--Module-->
<dependency>
<groupId>lm.solution</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>lm.solution</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <!--Libary-->
<!--spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--cglib-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!--mybatis spring 插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- Mysql数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<!--<scope>runtime</scope>-->
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 映入JSON lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
</dependency>
<!-- 用dom4j解析xml文件 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- common others -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- org.apache.httpcomponents -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency>
<!-- com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<finalName>web</finalName> <plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、web 配置
在 java config 配置方式中,我们就不需要在 web.xml 中配置监听及servlet 等节点了,这里我们仍然使用 servlet 3.0 API 。
1.web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
</web-app>
三、用 JavaConfig 替代 web.xml 中 servlet 配置及监听配置
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer:
WebInitializer继承关系链:
WebInitializer -->AbstractAnnotationConfigDispatcherServletInitializer -->AbstractDispatcherServletInitializer -->
AbstractContextLoaderInitializer -->WebApplicationInitializer
实现 WebApplicationInitializer 接口,就等同于 web.xml 配置
@Override public void onStartup :
web.xml 核心的相关配置,可再此方法中重写 自定义的配置 ,如:<filter> 可用 FilterRegistration.Dynamic encodingFilter 此种方式来配置。
@Override protected Class<?>[] getRootConfigClasses :
重写此方法,以替代 spring-context.xml ,在这个方法中返回的 类类型数组 可配置上下文中需要的 bean ,如数据库,缓存 等等。
@Override protected Class<?>[] getServletConfigClasses:
重写此方法,以替代 spring-mvc.xml ,在这个方法中返回的类型数组 可配置 spring 中 视图解析器,http message converter 等框架需要的 bean。
@Override protected String[] getServletMappings :
重写此方法,就相当于 <servlet-mapping> 节点等同的功能,可指定 站点根路径。
public class WebConfig extends WebMvcConfigurerAdapter :
@EnableWebMvc:注解会开启一些默认配置,如:ViewResolver MessageConverter 等,若无此注解,重写 WebMvcConfigurerAdapter 方法无效。
@ComponentScan:会扫描指定包中的类,根据注解,将特定类初始化为 上下文中需要的 bean 。
@Override public void addViewControllers :
重写 addViewControllers 简化页面快捷转向,这样就可以不用配置 Controller 了(就是那些仅返回一个页面路径的简单 controller )。
@Bean public InternalResourceViewResolver viewResolver :
配置JSP视图解析器
@Bean public MultipartResolver multipartResolver :
配置springMVC处理上传文件的信息
@Override public void addResourceHandlers :
配置静态文件处理
1.WebInitializer.java
package lm.solution.web.config; import lm.solution.web.config.configs.RootConfig;
import lm.solution.web.config.configs.WebConfig;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import javax.servlet.*; /**
* WebInitializer继承关系链:
* WebInitializer -->
* AbstractAnnotationConfigDispatcherServletInitializer -->
* AbstractDispatcherServletInitializer -->
* AbstractContextLoaderInitializer -->
* WebApplicationInitializer
* 实现 WebApplicationInitializer 接口,就等同于 web.xml 配置
* */
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
public void onStartup(ServletContext servletContext)
throws ServletException{ /**
* super 中已设置 servlet.setLoadOnStartup(1)
* */
super.onStartup(servletContext); //
ServletRegistration servletRegistration =servletContext.getServletRegistration("dispatcher"); // spring 字符过滤
FilterRegistration.Dynamic encodingFilter=servletContext.addFilter("encoding-filter",CharacterEncodingFilter.class);
encodingFilter.setInitParameter("encoding","UTF-8");
encodingFilter.setInitParameter("forceEncoding","true");
encodingFilter.setAsyncSupported(true);
encodingFilter.addMappingForUrlPatterns(null,true,"/*"); } // root配置类初始化
// 指定 Root WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件
@Override
protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class}; } // web配置类初始化
// 指定 Servlet WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件
@Override
protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class}; } // 映射根路径初始化
// 指定 Servlet mappings
@Override
protected String[] getServletMappings() { return new String[]{"/"}; } @Override
protected Filter[] getServletFilters() { return null; } @Override
protected void customizeRegistration(ServletRegistration.Dynamic registration){ // 让404作为异常抛出 不使用tomcat默认的
registration.setInitParameter("throwExceptionIfNoHandlerFound","true"); } }
2.WebConfig.java
package lm.solution.web.config.configs; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lm.solution.common.web.messageconverter.CustomMessageConverter;
import lm.solution.web.config.beans.TimerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; import java.util.ArrayList;
import java.util.List; @Configuration
/**
* @EnableWebMvc 注解会开启一些默认配置,如:ViewResolver MessageConverter 等,
* 若无此注解,重写 WebMvcConfigurerAdapter 方法无效
* */
@EnableWebMvc
@ComponentScan(value = {
"lm.solution.web",
"lm.solution.service.mysql",
"lm.solution.service.webtest"
})
/**
* 继承 WebMvcConfigurerAdapter 类,重写其方法可对 spring mvc 进行配置
* */
public class WebConfig extends WebMvcConfigurerAdapter { // 重写 addViewControllers 简化页面快捷转向,这样就可以不用配置 Controller 了
@Override
public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index");
registry.addViewController("/error").setViewName("error/error");
registry.addViewController("/excel").setViewName("excel/excel");
// 文件上传下载
registry.addViewController("/upload").setViewName("fileupload/upload");
registry.addViewController("/ImageValidateCodeLogin").setViewName("login/imageValidateCodeLogin");
registry.addViewController("/restfulapi").setViewName("restful/user");
registry.addViewController("/jaxwsri").setViewName("jaxwsri/wsri");
registry.addViewController("/redis").setViewName("redis/jedis");
registry.addViewController("/mybatisPage").setViewName("db/mybatis");
registry.addViewController("/messageconverter").setViewName("httpmessageconverter/customconverter");
registry.addViewController("/sse").setViewName("serverpushmessage/sse"); } // 配置JSP视图解析器
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
/**
* views 在 /resources/ 下
* */
// 前缀
viewResolver.setPrefix("/WEB-INF/classes/views/");
// 后缀
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
viewResolver.setContentType("text/html");
// 可以在JSP页面中通过${}访问beans
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
} // 配置springMVC处理上传文件的信息
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxUploadSize(10485760000L);
resolver.setMaxInMemorySize(40960);
return resolver;
} // 配置静态文件处理
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){ /**
* addResourceHandler 指的是对外暴露的访问路径
* addResourceLocations 指的是文件放置的目录
* */
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/assets/"); // href 链接方式 下载文件
registry.addResourceHandler("/files/**")
.addResourceLocations("classpath:/files/"); /**
* 解决 No handler found for GET /favicon.ico 异常
* */
registry.addResourceHandler("/favicon.ico")
.addResourceLocations("classpath:/favicon.ico"); } // 重写 configurePathMatch ,改变路径参数匹配
@Override
public void configurePathMatch(PathMatchConfigurer configurer) { /**
* Spring mvc 默认 如果路径参数后面带点,如 “/mm/nn/xx.yy” 后面的yy值将被忽略
* 加入下面的配置,就不会忽略“.”后面的参数了
* */
configurer.setUseSuffixPatternMatch(false); } //
// // 负责读取二进制格式的数据和写出二进制格式的数据;
// @Bean
// public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
//
// return new ByteArrayHttpMessageConverter();
//
// }
//
// // 负责读取字符串格式的数据和写出字符串格式的数据;
// @Bean
// public StringHttpMessageConverter stringHttpMessageConverter() {
//
// StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
// messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
// return messageConverter;
//
// }
//
// // 负责读取资源文件和写出资源文件数据;
// @Bean
// public ResourceHttpMessageConverter resourceHttpMessageConverter() {
//
// return new ResourceHttpMessageConverter();
//
// }
//
// /**
// * 负责读取form提交的数据
// * 能读取的数据格式为 application/x-www-form-urlencoded,
// * 不能读取multipart/form-data格式数据;
// * 负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
// */
// @Bean
// public FormHttpMessageConverter formHttpMessageConverter() {
//
// return new FormHttpMessageConverter();
//
// }
//
// // 负责读取和写入json格式的数据;
// /**
// * 配置 fastjson 中实现 HttpMessageConverter 接口的转换器
// * FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类
// */
// @Bean(name = "fastJsonHttpMessageConverter")
// public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
// //
// String txtHtml = "text/html;charset=UTF-8";
// String txtJson = "text/json;charset=UTF-8";
// String appJson = "application/json;charset=UTF-8";
//
// // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示
// List<MediaType> mediaTypes = new ArrayList<>();
// mediaTypes.add(MediaType.parseMediaType(txtHtml));
// mediaTypes.add(MediaType.parseMediaType(txtJson));
// mediaTypes.add(MediaType.parseMediaType(appJson));
//
// // 加入支持的媒体类型,返回 contentType
// FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
// fastjson.setSupportedMediaTypes(mediaTypes);
// return fastjson;
//
// }
//
// // 负责读取和写入 xml 中javax.xml.transform.Source定义的数据;
// @Bean
// public SourceHttpMessageConverter sourceHttpMessageConverter() {
//
// return new SourceHttpMessageConverter();
//
// }
//
// // 负责读取和写入xml 标签格式的数据;
// @Bean
// public Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter() {
//
// return new Jaxb2RootElementHttpMessageConverter();
//
// }
//
// // 注册消息转换器
// /**
// * 重写 configureMessageConverters 会覆盖掉 spring mvc 默认注册的多个 HttpMessageConverter
// * 所以 推荐使用 extendMessageConverter
// * */
// /**
// * Error:
// * 400:(错误请求) 服务器不理解请求的语法。
// * 415:(不支持的媒体类型) 请求的格式不受请求页面的支持。
// */
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
// converters.add(this.byteArrayHttpMessageConverter());
// converters.add(this.stringHttpMessageConverter());
// converters.add(this.resourceHttpMessageConverter());
// converters.add(this.formHttpMessageConverter());
// converters.add(this.fastJsonHttpMessageConverter());
// converters.add(this.sourceHttpMessageConverter());
// converters.add(this.jaxb2RootElementHttpMessageConverter());
//
// } // 自定义 HttpMessageConverter
@Bean
public CustomMessageConverter customMessageConverter(){ return new CustomMessageConverter(); } // 负责读取和写入json格式的数据;
/**
* 配置 fastjson 中实现 HttpMessageConverter 接口的转换器
* FastJsonHttpMessageConverter 是 fastjson 中实现了 HttpMessageConverter 接口的类
*/
@Bean(name = "fastJsonHttpMessageConverter")
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
//
String txtHtml = "text/html;charset=UTF-8";
String txtJson = "text/json;charset=UTF-8";
String appJson = "application/json;charset=UTF-8"; // 这里顺序不能反,一定先写 text/html,不然 IE 下会出现下载提示
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.parseMediaType(txtHtml));
mediaTypes.add(MediaType.parseMediaType(txtJson));
mediaTypes.add(MediaType.parseMediaType(appJson)); // 加入支持的媒体类型,返回 contentType
FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
fastjson.setSupportedMediaTypes(mediaTypes);
return fastjson; } /**
* 重写 extendMessageConverters 方法,仅添加自定义 HttpMessageConverter
* 不覆盖默认注册的 HttpMessageConverter
* */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(customMessageConverter());
converters.add(fastJsonHttpMessageConverter()); } // 拦截器 bean
@Bean
public TimerInterceptor timerInterceptor(){ return new TimerInterceptor(); } // 重写 addInterceptors 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timerInterceptor()); } }
3.RootConfig.java
package lm.solution.web.config.configs; import lm.solution.web.config.beans.MySqlBean;
import lm.solution.web.config.beans.RedisBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import lm.solution.web.config.beans.HandlerBean; @Configuration
@Import({
RedisBean.class,
MySqlBean.class,
HandlerBean.class
})
public class RootConfig {
/**
* 此处管理 Beans
* */
}
蒙
2018-05-20 23:46 周日