新增 spring boot 整合 servlet 用例

This commit is contained in:
luoxiang
2018-12-30 18:51:34 +08:00
parent c45d1d3b7a
commit 85f4d1a591
32 changed files with 764 additions and 22 deletions

View File

@ -0,0 +1,29 @@
package com.heibaiying.springbootservlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
@ServletComponentScan("com.heibaiying.springbootservlet") //在独立的容器(非内嵌)中@ServletComponentScan不起作用可以不配置取为代之的是容器内建的discovery机制自动发现。
public class SpringBootServletApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//传入SpringBoot应用的主程序
return application.sources(SpringBootServletApplication.class);
}
/**
* 如果用外置tomcat,启动报错java.lang.NoClassDefFoundError: javax/el/ELManager
* 是因为tomcat 7.0 el-api包中没有ELManager类 , 切换tomcat 为8.0 以上版本即可
*/
public static void main(String[] args) {
SpringApplication.run(SpringBootServletApplication.class, args);
}
}

View File

@ -0,0 +1,40 @@
package com.heibaiying.springbootservlet.config;
import com.heibaiying.springbootservlet.filter.CustomFilter;
import com.heibaiying.springbootservlet.listen.CustomListen;
import com.heibaiying.springbootservlet.servlet.CustomServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
/**
* @author : heibaiying
*/
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean registrationBean() {
return new ServletRegistrationBean<HttpServlet>(new CustomServlet(), "/servlet");
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean<Filter>();
bean.setFilter(new CustomFilter());
bean.addUrlPatterns("/servlet");
return bean;
}
@Bean
public ServletListenerRegistrationBean listenerRegistrationBean() {
return new ServletListenerRegistrationBean<ServletContextListener>(new CustomListen());
}
}

View File

@ -0,0 +1,30 @@
package com.heibaiying.springbootservlet.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* @author : heibaiying
* @description : 自定义过滤器
*/
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setAttribute("filterParam","我是filter传递的参数");
chain.doFilter(request,response);
response.getWriter().append(" CustomFilter ");
}
@Override
public void destroy() {
}
}

View File

@ -0,0 +1,30 @@
package com.heibaiying.springbootservlet.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* @author : heibaiying
* @description : 自定义过滤器
*/
@WebFilter(urlPatterns = "/servletAnn")
public class CustomFilterAnnotation implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request,response);
response.getWriter().append(" CustomFilter Annotation");
}
@Override
public void destroy() {
}
}

View File

@ -0,0 +1,27 @@
package com.heibaiying.springbootservlet.listen;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @author : heibaiying
* @description : 自定义监听器
*/
public class CustomListen implements ServletContextListener {
//Web应用程序初始化过程正在启动的通知
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("容器初始化启动");
}
/* 通知servlet上下文即将关闭
* 这个地方如果我们使用的是spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("容器即将销毁");
}
}

View File

@ -0,0 +1,29 @@
package com.heibaiying.springbootservlet.listen;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @author : heibaiying
* @description :自定义监听器
*/
@WebListener
public class CustomListenAnnotation implements ServletContextListener {
//Web应用程序初始化过程正在启动的通知
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("容器初始化启动 Annotation");
}
/* 通知servlet上下文即将关闭
* 这个地方如果我们使用的是spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("容器即将销毁 Annotation");
}
}

View File

@ -0,0 +1,25 @@
package com.heibaiying.springbootservlet.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author : heibaiying
* @description : 自定义servlet
*/
public class CustomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet 执行:" + req.getAttribute("filterParam"));
resp.getWriter().append("CustomServlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

View File

@ -0,0 +1,26 @@
package com.heibaiying.springbootservlet.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author : heibaiying
* @description : 自定义servlet
*/
@WebServlet(urlPatterns = "/servletAnn")
public class CustomServletAnnation extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().append("CustomServlet Annotation");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

View File

@ -0,0 +1,17 @@
package com.heibaiying.springbootservlet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootServletApplicationTests {
@Test
public void contextLoads() {
}
}