I want to add spring mvc interceptor as part of Java config. I already have a xml based config for this but I am trying to move to a Java config. For interceptors, I know that it can be done like this from the spring documentation-
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor());
}
}
But my interceptor is using a spring bean autowired into it like follows-
public class LocaleInterceptor extends HandlerInterceptorAdaptor {
@Autowired
ISomeService someService;
...
}
The SomeService class looks like follows-
@Service
public class SomeService implements ISomeService {
...
}
I am using annotations like @Service for scanning the beans and have not specified them in the configuration class as @Bean
As my understanding, since java config uses new for creating the object, spring will not automatically inject the dependencies into it.
How can I add the interceptors like this as part of the java config?
Answers
| 34down voteaccepted | Just do the following: Of course The documentation for general customization of Spring's MVC configuration can be found here, and specifically for Interceptors see this section |
本文介绍如何在Java配置中设置Spring MVC拦截器,并解决拦截器依赖注入的问题。通过@Bean注解定义拦截器实例,确保Spring能够正确地管理依赖关系。
837

被折叠的 条评论
为什么被折叠?



