& lt;属性名="拦截"比;& lt; list>& lt;裁判bean = " validatorInteceptor "/比;& lt;裁判bean = " profilingInterceptor "/比;& lt;/list>& lt;/property>& lt;/bean>& lt;/beans>之前
是否可以使用注释驱动的注入来做同样的事情呢?
<>之前& lt; beans>…是否有可能使用注释驱动的注入做同样的事情?
好问题-我不这么认为(假设"注释驱动的注入"你指的是AnyAction
上的注释)。
下面可能会工作,但我不认为Spring识别@Resources
注释:
@Resources({
@Resource(name="validatorInteceptor"),
@Resource(name="profilingInterceptor")
})
private List interceptors;
试一试吧,你永远不会知道。
除此之外,您可以使用@Configuration
样式的配置而不是XML:
@Configuration
public class MyConfig {
private @Resource Interceptor profilingInterceptor;
private @Resource Interceptor validatorInteceptor;
@Bean
public AnyAction anyAction() {
AnyAction anyAction = new AnyAction();
anyAction.setInterceptors(Arrays.asList(
profilingInterceptor, validatorInteceptor
));
return anyAction;
}
}
是的,如果你使用这种模式,Spring会很高兴地注入所有配置好的拦截器:
@Autowired
public void setInterceptors(List<Interceptor> interceptors){
this.interceptors = interceptors;
}
private List<Interceptor> interceptors;
注意,您可能必须在您的context.xml中配置default-autowire=byType。我不知道在普通注释配置中是否有替代方法