Я прочитал эту статью, в нем говорится, что бобы с сопоставлением не будут работать в простой весне, но он помогает мне вызвать перехватчик здесь. Хотя я думаю, что сейчас я использую простую пружину.
Код: Выделить всё
@Configuration
@EnableWebMvc
public class InterceptorConfig implements WebMvcConfigurer {
private final LoginAnnotationInterceptor loginAnnotationInterceptor;
public InterceptorConfig(LoginAnnotationInterceptor loginAnnotationInterceptor) {
this.loginAnnotationInterceptor = loginAnnotationInterceptor;
}
// the interceptor added here cannot be invoked
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginAnnotationInterceptor);
}
// the interceptor added here is able to be invoked
@Bean
public MappedInterceptor loginAnnotationInterceptorBean()
{
return new MappedInterceptor(null, loginAnnotationInterceptor);
}
}
< /dode>
loginannotationIntereptor: < /p>
@Component
public class LoginAnnotationInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("It's an interceptor!");
// some logic...
return true;
}
}
< /code>
Это моя либерация ... не знаю, здесь что -то не так, < /p>
Это мой web.xml. Контекст: компонент-сканирование/ часть для сканирования The InterceptorConfig определяется в ApplicationContext.xml. Я считаю, что моя конфигурация загружена в DispatcherServlet.
contextConfigLocation
/WEB-INF/root-context.xml
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/applicationContext.xml
1
< /code>
applicationcontext.xml:
Подробнее здесь: https://stackoverflow.com/questions/796 ... ot-working