SpringBoot整合Web层技术
1 SpringBoot整合Web层技术
1.1 SpringBoot整合Servlet
目录结构
1.1.1 方式一 通过注解扫描完成Servlet组件的注册
1.1.1.1 创建Servlet
/**
* SpringBoot 整合servlet方式一
*/
@WebServlet(name = "FirstServlet" , urlPatterns = {"/first"})
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
System.out.println("First Servlet......");
}
}
需要在Servlet类上加上
@WebServlet
注解,并指定name
属性和urlPatterns
属性
1.1.1.2 修改启动类
@SpringBootApplication
@ServletComponentScan //在Spring Boot启动时会扫描@WebServlet注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
启动类需要加上ServletComponentScan注解
1.1.2 方式二 通过方法完成Servlet组件的注册
1.1.2.1 创建Servlet
/**
* SpringBoot 整合servlet方式二
*/
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Second Servlet......");
}
}
需要继承HttpServlet
1.1.2.2 创建Servlet配置类
/**
* 完成Servlet组件的注册
*/
@Configuration
public class ServletConfig {
/**
* 容器开始的时候初始化一次
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet(),"/second");
bean.addUrlMappings("/second2");
bean.addUrlMappings("/second3");
return bean;
}
}
需要在配置类上加上
@Configuration
注解,在方法上加上@Bean
注解,返回ServletRegistrationBean
对象
1.1.2.3 启动类
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
1.2 SpringBoot整合Filter
1.2.1 方式一 通过注解扫描完成Filter组件注册
1.2.1.1 创建Filter
//@WebFilter(filterName = "FirstFilter",urlPatterns = {"*.do","*.jsp"})
@WebFilter(filterName = "FirstFilter",urlPatterns = "/first")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入first Filter..");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("退出first Filter..");
}
@Override
public void destroy() {
}
}
Filter类需要添加
@WebFilter
注解,实现Filter
接口(javax.servlet.*)
1.2.1.2 修改启动类
@SpringBootApplication
//@ServletComponentScan //在Spring Boot启动时会扫描@WebServlet、@WebFilter注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
1.2.2 方式二 通过方法完成Filter组件注册
1.2.2.1 创建Filter
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入second Filter..");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("退出second Filter..");
}
@Override
public void destroy() {
}
}
1.2.2.2 创建Filter配置类
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean getFilterRegistration(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new SecondFilter());
filterRegistrationBean.addUrlPatterns("/second");
return filterRegistrationBean;
}
}
配置类需要加上
@Configuration
注解,方法加上@Bean
注解,返回FilterRegistrationBean
对象
1.2.1.3 修改启动类
@SpringBootApplication
@ServletComponentScan //在Spring Boot启动时会扫描@WebServlet、@WebFilter注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
启动类需要加上
@ServletComponentScan
注解,这样可以扫描@WebFilter
注解
1.3 SpringBoot整合Listener
1.3.1 方式一 通过注解扫描完成Listener组件注册
1.3.1.1 创建Listener
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("first listener ... init");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("first listener ... destroy");
}
}
Listener类需要加上
@WebListener
注解
1.3.1.2 修改启动类
@SpringBootApplication
@ServletComponentScan //在Spring Boot启动时会扫描@WebServlet、@WebFilter、@WebListener注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
1.3.2 方式二 通过方法完成Listener组件注册
1.3.2.1 创建Listener
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("second listener ... init");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("second listener ... destroy");
}
}
1.3.2.2 创建配置类
@Configuration
public class ListenerConfig {
@Bean
public ServletListenerRegistrationBean getListener(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}
类加上
@Configuration
注解,方法加上@Bean
注解
1.3.2.3 修改启动类
@SpringBootApplication
//@ServletComponentScan //在Spring Boot启动时会扫描@WebServlet、@WebFilter、@WebListener注解,并将该类实例化
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------