在包 com.example.demo.filter
下新建 FilterFilter.java
文件:
package com.example.demo.filter;
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* @author Where
*/
@Order(1)
@WebFilter(filterName = "FirstFilter", urlPatterns = "/")
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("FirstFilter");
}
@Override
public void destroy() {
}
}
在 Application.java
上添加 @ServletComponentScan
注解:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* Application
*
* @author Where
*/
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
重新启动应用,访问 http://localhost:8080
查看控制台。