Java @自动连线在EndpointInterceptor中不工作
我有一个定制的Java @自动连线在EndpointInterceptor中不工作,java,spring-ws,Java,Spring Ws,我有一个定制的EndpointInterceptor实现 @Component public class MyEndpointInterceptor implements EndpointInterceptor { @Autowired private Jaxb2Marshaller marshaller; @Override public boolean handleRequest(MessageContext messageContext, Object o) throws Except
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的config类中
@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());
}
}
@配置
@使能
公共类MyWebServiceConfiguration扩展了WsConfigurerAdapter{
@Bean(name=“marshaller”)
公共Jaxb2Marshaller createJaxb2Marshaller(){
Jaxb2Marshaller-marshaller=新的Jaxb2Marshaller();
返回编组员;
}
@凌驾
公共void附加侦听器(列表侦听器)
{
//寄存器拦截器
add(newmyendpointinterceptor());
}
}
但是marshaller
对象是null
现在我还缺少什么吗?如果您使用构造函数注入而不是字段注入,您可能会得到一个非常有用的异常,我只能猜测,但Spring在Spring上下文中似乎没有封送处理程序,因此您需要在某个地方提供@Bean方法,例如
@Bean
public Jaxb2Marshaller jaxb2Marshaller () {
return new Jaxb2Marshaller(foo, bar, ..);
}
您可以在此处阅读为什么应尽量避免现场注入:
如果您使用构造函数注入而不是字段注入,您可能会得到一个非常有用的异常,我只能猜测,但Spring在Spring上下文中似乎没有封送处理程序,因此您需要在某个地方提供@Bean方法,例如
@Bean
public Jaxb2Marshaller jaxb2Marshaller () {
return new Jaxb2Marshaller(foo, bar, ..);
}
您可以在此处阅读为什么应尽量避免现场注入:
您的问题是您不让spring管理MyEndpointInterceptor。使用Spring时,不应直接使用构造函数。但是,让春天为你建造豆子吧 您的配置应该如下所示:
@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);
}
}
@配置
@使能
公共类MyWebServiceConfiguration扩展了WsConfigurerAdapter{
@Bean(name=“marshaller”)
公共Jaxb2Marshaller createJaxb2Marshaller(){
Jaxb2Marshaller-marshaller=新的Jaxb2Marshaller();
返回编组员;
}
@自动连线
私有MyEndpointInterceptor MyEndpointInterceptor;
@凌驾
公共void附加侦听器(列表侦听器)
{
//寄存器拦截器
add(myEndpointInterceptor);
}
}
您的问题是不让spring管理MyEndpointInterceptor。使用Spring时,不应直接使用构造函数。但是,让春天为你建造豆子吧
您的配置应该如下所示:
@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);
}
}
@配置
@使能
公共类MyWebServiceConfiguration扩展了WsConfigurerAdapter{
@Bean(name=“marshaller”)
公共Jaxb2Marshaller createJaxb2Marshaller(){
Jaxb2Marshaller-marshaller=新的Jaxb2Marshaller();
返回编组员;
}
@自动连线
私有MyEndpointInterceptor MyEndpointInterceptor;
@凌驾
公共void附加侦听器(列表侦听器)
{
//寄存器拦截器
add(myEndpointInterceptor);
}
}
你能在你的问题中添加配置类吗?嗨@Sodala,我已经添加了配置你能在你的问题中添加配置类吗?嗨@Sodala,我已经添加了配置我已经更新了问题,在上下文中创建了一个bean我已经更新了问题,在上下文中创建了一个bean