摘要:本文主要介绍了在SpringBoot中如何加载XML配置文件,以及如何注册拦截器。
加载XML配置
其实在SpringBoot中加载XML配置有点鸡肋,因为SpringBoot一般是不用XML配置的,但是在这里我们还是说明一下吧。首先我们创建一个SayHello类,但是不为其添加注解。
1 2 3 4 5 6 7 8 9 10 11 12
| package com.example.xml;
public class SayHello { public String sayHello() { return "hello"; } }
|
然后创建一个XML配置文件
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.example.xml.SayHello" id="sayHello"/> </beans>
|
最后再创建一个配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.example.xml;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource;
@Configuration @ImportResource(locations = "classpath:beans.xml") public class WebMvcConfig { }
|
那么至此XML配置就已经加载完成了,我们再通过单元测试来看看是否成功加载了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.xml;
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest class XmlApplicationTests { @Autowired SayHello sayHello;
@Test void contextLoads() { System.out.println(sayHello.sayHello()); } }
|
注册拦截器
首先创建一个自定义拦截器类,然后实现HandlerInterceptor接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.example.interceptor;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle"); return true; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
|
再创建一个配置类来注册自定义的拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.example.interceptor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor()).addPathPatterns("/**"); }
@Bean MyInterceptor myInterceptor(){ return new MyInterceptor(); } }
|
最后呢创建一个HelloController来进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.example.interceptor;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController { @GetMapping("/hello") public String hello(){ return "hello"; } }
|
即可看到拦截器的方法被调用了