@Autowired 在 EndpointInterceptor 中不起作用
@Autowired not working in EndpointInterceptor
我有一个自定义 EndpointInterceptor
实现;
@Component
public class MyEndpointInterceptor implements EndpointInterceptor {
@Autowired
private Jaxb2Marshaller marshaller;
@Override
public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
// ... do stuff with marshaller
}
}
拦截器添加到扩展 WsConfigurerAdapter
的配置 class 中;
@Configuration
@EnableWs
public class MyWebServiceConfiguration extends WsConfigurerAdapter {
@Bean(name = "marshaller")
public Jaxb2Marshaller createJaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
return marshaller;
}
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)
{
// Register interceptor
interceptors.add(new MyEndpointInterceptor());
}
}
但是 marshaller
对象是 null
。
此时我还遗漏了什么吗?
如果你使用构造函数注入而不是字段注入,你可能会得到一个非常有用的异常,我只能猜测,但似乎 Spring 在 Spring 上下文中没有编组器,因此你需要在某处提供@Bean 方法,例如
@Bean
public Jaxb2Marshaller jaxb2Marshaller () {
return new Jaxb2Marshaller(foo, bar, ..);
}
您可以在此处阅读为什么应尽量避免字段注入:
https://www.vojtechruzicka.com/field-dependency-injection-considered-harmful/
您的问题是您没有让 spring 管理 MyEndpointInterceptor。使用Spring时,不应该直接使用构造函数。但是让 Spring 为您构建 bean。
您的配置应如下所示:
@Configuration
@EnableWs
public class MyWebServiceConfiguration extends WsConfigurerAdapter {
@Bean(name = "marshaller")
public Jaxb2Marshaller createJaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
return marshaller;
}
@Autowired
private MyEndpointInterceptor myEndpointInterceptor;
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)
{
// Register interceptor
interceptors.add(myEndpointInterceptor);
}
}
我有一个自定义 EndpointInterceptor
实现;
@Component
public class MyEndpointInterceptor implements EndpointInterceptor {
@Autowired
private Jaxb2Marshaller marshaller;
@Override
public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
return true;
}
@Override
public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
// ... do stuff with marshaller
}
}
拦截器添加到扩展 WsConfigurerAdapter
的配置 class 中;
@Configuration
@EnableWs
public class MyWebServiceConfiguration extends WsConfigurerAdapter {
@Bean(name = "marshaller")
public Jaxb2Marshaller createJaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
return marshaller;
}
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)
{
// Register interceptor
interceptors.add(new MyEndpointInterceptor());
}
}
但是 marshaller
对象是 null
。
此时我还遗漏了什么吗?
如果你使用构造函数注入而不是字段注入,你可能会得到一个非常有用的异常,我只能猜测,但似乎 Spring 在 Spring 上下文中没有编组器,因此你需要在某处提供@Bean 方法,例如
@Bean
public Jaxb2Marshaller jaxb2Marshaller () {
return new Jaxb2Marshaller(foo, bar, ..);
}
您可以在此处阅读为什么应尽量避免字段注入: https://www.vojtechruzicka.com/field-dependency-injection-considered-harmful/
您的问题是您没有让 spring 管理 MyEndpointInterceptor。使用Spring时,不应该直接使用构造函数。但是让 Spring 为您构建 bean。
您的配置应如下所示:
@Configuration
@EnableWs
public class MyWebServiceConfiguration extends WsConfigurerAdapter {
@Bean(name = "marshaller")
public Jaxb2Marshaller createJaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
return marshaller;
}
@Autowired
private MyEndpointInterceptor myEndpointInterceptor;
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)
{
// Register interceptor
interceptors.add(myEndpointInterceptor);
}
}