实现 WebApplicationInitializer 接口来实现动态注册Servlet 就觉得比较有意思 值得试试看;
首先万变不离其宗POM 依赖如下;
4.0.0
com.javen.maven02
maven02
war
0.0.1-SNAPSHOT
4.3.2.RELEASE
3.2.6
1.7.7
1.2.17
junit
junit
4.11
test
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-core
${spring.version}
org.jsoup
jsoup
1.9.2
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
com.alibaba
druid
1.1.9
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.36
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.4
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
com.fasterxml.jackson.core
jackson-databind
2.7.4
com.fasterxml.jackson.core
jackson-core
2.7.4
com.fasterxml.jackson.core
jackson-annotations
2.7.4
com.github.penggle
kaptcha
2.3.2
javax.mail
mail
1.4.5
io.springfox
springfox-swagger2
2.8.0
io.springfox
springfox-swagger-ui
2.8.0
org.springframework.data
spring-data-redis
1.7.2.RELEASE
redis.clients
jedis
2.8.1
org.apache.jmeter
ApacheJMeter_java
3.3
SpringMVCmybatis
org.apache.maven.plugins
maven-compiler-plugin
3.3
1.8
你需要有一个这样的工程目录结构 java 是源码 resources主要存储一些配置文件 webapp 主要存放页面文件和前台文件;
第一步:首先实现动态注册首先要实现WebApplicationInitializer 或者继承
AbstractAnnotationConfigDispatcherServletInitializer类
package com.javen.Config;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
*
* servlet3.0以上支持
* @author Administrator
*
*/
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
private final static Logger LOG = Logger.getLogger(WebAppInitializer.class);
@Override
protected Class>[] getRootConfigClasses() {
LOG.info("------root配置类初始化------");
return new Class>[] { RootConfig.class };
}
@Override
protected Class>[] getServletConfigClasses() {
LOG.info("------web配置类初始化------");
return new Class>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
LOG.info("------映射根路径初始化------");
return new String[]{ "/" };//请求路径映射,根路径
}
}
第二步:编写JavaConfig风格的配置文件类;
RootConfig.class 指的是Spring的默认配置文件类也就是之前的applicationContex.xml
有一些需要优先初始化的类 可以在这个类中写
@Configuration
@ComponentScan(basePackages="com.javen.*")
public class RootConfig {
@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List resourceLst = new ArrayList();
resourceLst.add(new ClassPathResource("jdbc.properties"));
resourceLst.add(new ClassPathResource("mail.properties"));
resourceLst.add(new ClassPathResource("redis.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[] {}));
return ppc;
}
WebConfig.class 需要继承WebMvcConfigurerAdapter 指的是Spring mvc 的配置文件类
@Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan( "com.javen.controller" ) public class WebConfig extends WebMvcConfigurerAdapter{ @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/page/"); resolver.setSuffix(".html"); return resolver; } @Bean(name="multipartResolver") protected CommonsMultipartResolver MultipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); //multipartResolver.setUploadTempDir(new FileSystemResource("/tmp"));//可以不设置 multipartResolver.setMaxUploadSize(2097152);//2M multipartResolver.setMaxInMemorySize(0); multipartResolver.setDefaultEncoding("UTF-8"); return multipartResolver; } public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new Mvcinterceptor()).addPathPatterns("/**"); } }
原理解析:
@HandlesTypes(WebApplicationInitializer.class) public class SpringServletContainerInitializer implements ServletContainerInitializer {]
ServletContainerInitializer 这个类是Servlet提供的接口
@HandlesTypes(WebApplicationInitializer.class)是一个注解说引入这个接口
运行步骤
1、tomcat启动后会去找实现ServletContainerInitializer接口2、这时候他找到 SpringServletContainerInitializer 但是发现实现类需要扫描
@HandlesTypes(WebApplicationInitializer.class)然后将扫描结果返回给SpringServletContainerInitializer
3、我们的入口类实现WebApplicationInitializer所以自然会被找到并动态注册;
详细的源码可以转至:https://github.com/379753498/SpringMVCmybatis