I am currently learning about spring and the concept of beans. I learned that in spring project you can configure in 3 ways -- xml, annotation-based and java-based. I am trying to configure my interceptor in all ways but the @Configuration way fails, the interceptor cannot be invoked this way. I don't know why the @Bean MappedInterceptor configuration invoke the interceptor, all I know is that there are probably some auto configuring behind the spring but I can't find any articles about it...
I've read this article, it says the mappedInterceptor bean won't work in plain spring but it helps me invoke the interceptor here. Although I think I am using plain spring now.
I've also read this article
InterceptorConfig:
@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);
}
}
LoginAnnotationInterceptor:
@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;
}
}
this is my lib...don't know if anything is wrong here
This is my web.xml. The context:component-scan/ part to scan the InterceptorConfig is defined in applicationContext.xml. I believe my configuration is loaded in DispatcherServlet.
web.xml:
<!-- Root Context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext.xml:
<context:component-scan base-package="org.com.interceptor"/>
I tried to print out the beans in the ApplicationContext but still doesn't help, is there any other way to debug this problem or understand the reason behind it?
MappedInterceptorwill probably execute and if thepreHandlereturnsfalseit will not invoke the next interceptors. Next to that how and where is this configuration being loaded. If it is in the ROOT context (theContextLoaderListenerthe@EnableWebMVcis pretty much useless and youraddInterceptorwill ultimatly be ignored. Where as theMappedInterceptorwill still be detected as it detectsMappedInterrceptorsin the parent. TheInterceptorConfigshould be loaded by theDispatcherServletand not theContextLoaderListenerroot-context.xmltoapplicationContext.xml(default loaded byContextLoaderListener) andapplicationContext.xmltodispatcher-servlet.xml(which will then be automatically loaded by theDispatcherServlet. Another thing is why XML why not simply java based config and aWebApplicationInitializerto bootstrap your app ...ContextLoaderListenerlevel and not theDispatcherServletlevel.