要保证两点:
org.springframework.boot
spring-boot-starter-test
test
@RunWith(SpringRunner.class)
// classes用于指定被测内容的启动类
@SpringBootTest(classes = Application09Test.class)
public class Application09TestTests {
@Autowired
private SomeService service;
@Test
public void doSome() {
System.out.println(service.doSome());
}
}
public class SomeIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
System.out.println("执行拦截器");
return true;
}
}
@Configuration // 表示当前类充当着Spring配置文件
public class MyConfig extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SomeIntercepter())
// .addPathPatterns("/**/some"); // 表示拦截所有以/some结尾的请求
// .addPathPatterns("/test/**"); // 表示拦截所有以/test开头的请求
// 除了/other结尾的请求外,拦截所有请求
.addPathPatterns("/**").excludePathPatterns("/**/other");
}
}
适用于Servlet2.5及Servlet3.0及以上版本
public class SomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("spring boot servlet");
}
}
@Configuration
public class ConsumerApplicationContext {
@Bean
public ServletRegistrationBean getServletBean() {
SomeServlet someServlet = new SomeServlet();
return new ServletRegistrationBean(someServlet, "/some");
}
}
仅适用于Servlet3.0及其以上版本
@WebServlet("/some")
public class SomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("spring boot servlet");
}
}
// 需要指定要扫描的Servlet所在的包路径
@ServletComponentScan("com.bjpowernode.servlet")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
适用于Servlet2.5及Servlet3.0及以上版本
public class SomeFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("执行过滤器");
chain.doFilter(req, resp);
}
}
@Bean
public FilterRegistrationBean getFilterBean() {
SomeFilter someFilter = new SomeFilter();
FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(someFilter);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
仅适用于Servlet3.0及其以上版本
@WebFilter("/*")
public class SomeFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("执行过滤器");
chain.doFilter(req, resp);
}
}
// 需要指定要扫描的Servlet及Filter所在的包路径
@ServletComponentScan("com.bjpowernode.*")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建工程时选择war,而非jar即可。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// 获取Spring容器对象
ConfigurableApplicationContext ac = SpringApplication.run(Application.class, args);
SomeService service = (SomeService) ac.getBean("someServiceImpl");
System.out.println(service.doSome());
}
}