Spring拦截器的Java配置,其中拦截器使用自动装配的spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23349180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java config for spring interceptor where interceptor is using autowired spring beans
提问by user3565529
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-
我想添加 spring mvc 拦截器作为 Java 配置的一部分。我已经有一个基于 xml 的配置,但我正在尝试转向 Java 配置。对于拦截器,我知道可以从 spring 文档中这样做 -
@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-
但是我的拦截器正在使用自动装配到其中的 spring bean,如下所示-
public class LocaleInterceptor extends HandlerInterceptorAdaptor {
@Autowired
ISomeService someService;
...
}
The SomeService class looks like follows-
SomeService 类如下所示 -
@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
我正在使用诸如@Service
扫描 bean 之类的注释,并且没有在配置类中将它们指定为@Bean
As my understanding, since java config uses new for creating the object, spring will not automatically inject the dependencies into it.
据我了解,由于 java config 使用 new 来创建对象,因此 spring 不会自动将依赖项注入其中。
How can I add the interceptors like this as part of the java config?
如何将这样的拦截器添加为 java 配置的一部分?
采纳答案by geoand
Just do the following:
只需执行以下操作:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
LocaleInterceptor localInterceptor() {
return new LocalInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeInterceptor());
}
}
Of course LocaleInterceptor
needs to be configured as a Spring bean somewhere (XML, Java Config or using annotations) in order for the relevant field of WebConfig
to get injected.
当然LocaleInterceptor
需要在某处(XML、Java Config 或使用注解)配置为 Spring bean,以便WebConfig
注入相关字段。
The documentation for general customization of Spring's MVC configuration can be found here, and specifically for Interceptors see thissection
回答by oko
Try to inject your service as a constructor parameter. It is simple.
尝试将您的服务作为构造函数参数注入。很简单。
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
ISomeService someService;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor(someService));
}
}
Then reconfigure your interceptor,
然后重新配置你的拦截器,
public class LocaleInterceptor extends HandlerInterceptorAdaptor {
private final ISomeService someService;
public LocaleInterceptor(ISomeService someService) {
this.someService = someService;
}
}
Cheers !
干杯!
回答by José Andias
When you handle the object creation for yourself like in:
当您为自己处理对象创建时,例如:
registry.addInterceptor(new LocaleInterceptor());
there is no way the Spring container can manage that object for you and therefore make the necessary injection into your LocaleInterceptor
.
Spring 容器无法为您管理该对象,因此无法将必要的注入到您的LocaleInterceptor
.
Another way that could be more convenient for your situation, is to declare the managed @Bean
in the @Configuration
and use the method directly, like so:
另一种方式,可能是您的情况更方便,正在申报管理@Bean
中@Configuration
直接使用的方法,就像这样:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleInterceptor localeInterceptor() {
return new LocaleInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor( localeInterceptor() );
}
}