SpringBoot框架——注册Servlet、Filter、Listener

5 篇文章 0 订阅
2 篇文章 0 订阅

SpringBoot注册Servlet、Filter、Listener

有两种方式:通过 @WebServlet 注释进行注册、通过方法进行注册。

方法1:通过 @WebServlet 注释进行注册

1、在Servlet类上添加 @WebServlet 注解
package com.springBootDemo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/***
 * 使用@WebServlet(name = "ServletByAnnotation", urlPatterns = "/servletByAnnotation")相当于xml文件中的配置
 * <servlet>
 * 		<servlet-name>ServletByAnnotation</servlet-name>
 * 		<servlet-class>com.springBootDemo.servlet.ServletByAnnotation</servlet-class>
 * </servlet>
 * <servlet-mapping>
 * 		<servlet-name>ServletByAnnotation</servlet-name>
 * 		<url-pattern>/servletByAnnotation</url-pattern>
 * <servlet-mapping>
 */
@WebServlet(name = "ServletByAnnotation", urlPatterns = "/servletByAnnotation")
public class ServletByAnnotation extends HttpServlet {

	private static final long serialVersionUID = 1920014204655473135L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
}
2、在启动类上添加 @ServletComponentScan 注解
/***
 * @ServletComponentScan注解,项目启动时会扫描所有的servlet(使用 @WebServlet 的类),并实例化
 */
@SpringBootApplication
@ServletComponentScan
public class Application {

	public static void main(String[] args) {

		// 通过run方法启动SpringBoot(参数1为启动类的字节码文件对象)
		SpringApplication.run(Application.class, args);

		// 程序启动完毕后打印
		System.out.println("SpringBoot is run !");
	}
}

方法2:通过方法进行注册

增加被 @Bean 注释的方法(loadServletByMethod),该方法的返回值为 ServletRegistrationBean
package com.springBootDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.springBootDemo.filter.FilterByMethod;
import com.springBootDemo.servlet.ServletByMethod;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        // 通过run方法启动SpringBoot(参数1为启动类的字节码文件对象)
        SpringApplication.run(Application.class, args);

        // 程序启动完毕后打印
        System.out.println("SpringBoot is run !");
    }

    // 在启动程序时,系统会自动加载@Bean注释的方法
    // 通过方法注册Servlet,(无需在启动类上添加 @ServletComponentScan 注释)
    @Bean
    public ServletRegistrationBean loadServletByMethod() {

        ServletRegistrationBean srb = new ServletRegistrationBean(
                new ServletByMethod());

        // 设置Servlet的url-pattern
        srb.addUrlMappings("/servletByMethod");

        return srb;
    }
}
package com.springBootDemo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletByMethod extends HttpServlet {

	private static final long serialVersionUID = 8336560210248210575L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
}


SpringBoot注册Filter

有两种方式:通过 @WebFilter 注释进行注册、通过方法进行注册。

方法1:通过@WebFilter 注释进行注册

添加 @WebFilter 注释,urlPatterns可以根据所需要拦截url的数量不同进行改变,字符串(单个)、字符数组(多个)

package com.springBootDemo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/***
 * 使用@WebFilter(filterName = "FilterByAnnotation", urlPatterns = { "/a/*", "/b/*" })就相当于:
 * <filter>
 * 		<filter-name>FilterByAnnotation</filter-name>
 * 		<filter-class>com.springBootDemo.filter.FilterByAnnotation</filter-class>
 * </filter>
 * <filter-mapping>
 * 		<filter-name>FilterByAnnotation</filter-name>
 * 		<url-pattern>/a/*</url-pattern>
 * </filter-mapping>
 * <filter-mapping>
 * 		<filter-name>FilterByAnnotation</filter-name>
 * 		<url-pattern>/b/*</url-pattern>
 * </filter-mapping>
 * 
 * urlPatterns值的类型,可以根据实际需要变更为 字符串 或者是 字符数组
 */
@WebFilter(filterName = "FilterByAnnotation", urlPatterns = { "/a/*", "/b/*" })
public class FilterByAnnotation implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}


方法2:通过方法进行注册

增加被 @Bean 注释的方法(loadFilterByMethod),该方法的返回值为 FilterRegistrationBean
package com.springBootDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.springBootDemo.filter.FilterByMethod;
import com.springBootDemo.servlet.ServletByMethod;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {

		// 通过run方法启动SpringBoot(参数1为启动类的字节码文件对象)
		SpringApplication.run(Application.class, args);

		// 程序启动完毕后打印
		System.out.println("SpringBoot is run !");
	}

	// 在启动程序时,系统会自动加载@Bean注释的方法
	// 通过方法注册Filter
	public FilterRegistrationBean loadFilterByMethod() {

		FilterRegistrationBean frb = new FilterRegistrationBean(
				new FilterByMethod());

		// 设置Filter的url-pattern(参数可以使字符串或字符数组)
		frb.addUrlPatterns(new String[] { "/a/*", "/b/*" });

		return frb;
	}
}
package com.springBootDemo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FilterByMethod implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

SpringBoot注册Listener

有两种方式:通过 @WebListener 注释进行注册、通过方法进行注册。

方法1:通过@WebListener 注释进行注册

添加 @WebListener 即可

package com.springBootDemo.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/***
 * 使用 @WebListener 就相当于:
 * <Listener>
 * 		<Listener-class>com.springBootDemo.listener.ListenerByAnaotation</Listener-class>
 * </Listener>
 */
@WebListener
public class ListenerByAnnotation implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

}


方法2:通过方法进行注册

增加被 @Bean 注释的方法(LoadListenerByMethod),该方法的返回值为 ServletListenerRegistrationBean,泛型为ListenerByMethod

package com.springBootDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.springBootDemo.Listener.ListenerByMethod;
import com.springBootDemo.filter.FilterByMethod;
import com.springBootDemo.servlet.ServletByMethod;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {

		// 通过run方法启动SpringBoot(参数1为启动类的字节码文件对象)
		SpringApplication.run(Application.class, args);

		// 程序启动完毕后打印
		System.out.println("SpringBoot is run !");
	}

	// 在启动程序时,系统会自动加载@Bean注释的方法
	// 通过方法注册Listener
	// (返回值为 ServletListenerRegistrationBean ,泛型为ListenerByMethod)
	@Bean
	public ServletListenerRegistrationBean<ListenerByMethod> loadListenerByMethod() {

		ServletListenerRegistrationBean<ListenerByMethod> slrb = new ServletListenerRegistrationBean<ListenerByMethod>(
				new ListenerByMethod());

		return slrb;
	}
}
package com.springBootDemo.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ListenerByMethod implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值