What you posted works, but you aren't taking advantage of Spring's autowiring capabilities. What if you had many services or other beans to inject into your MyInterceptor
bean.
Instead just make a @Bean
method for your MyInterceptor
@EnableWebMvc
@ComponentScan(basePackages = {"com.whatever"})
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserSession userSession() {
return new UserSession();
}
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor(); // let Spring go nuts injecting stuff
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()); // will always refer to the same object returned once by myInterceptor()
}
...
}