【问题标题】:Spring @Autowired is not working for @WebService annotated classSpring @Autowired 不适用于 @WebService 注释类
【发布时间】:2013-07-10 09:51:55
【问题描述】:

尝试自动装配时获取空指针。

创建一个 Web 应用程序并使用以下 Web 服务:

WebServiceEndpoint.java

@WebService
@Component
public class ChannelMapWebServiceEndpoint {

   @Autowired
   ChannelMapWebService webservice;



   public ChannelMapInfo4[] getMaps() throws RemoteException {
     return this.webservice.getMaps();
   }    

}

ChannelMapsebserviceImpl.java

@Service
public class ChannelMapWebServiceImpl implements ChannelMapWebService {

   public ChannelMapInfo4[] getMaps() throws RemoteException {
     System.out.println("hi");
   }

}

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="ccad" />
    <context:component-scan base-package="channelmapwebservice" />



    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>/WEB-INF/jdbc.properties</value>
        </property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

    </bean>

</beans>

在尝试通过 SoapUI 连接时,我将自动装配的对象 web 服务设为 null。

【问题讨论】:

标签: spring web-services dependency-injection


【解决方案1】:

为您的请求类服务的ChannelMapWebServiceEndpoint 对象不是由 Spring 实例化(和管理)的,这就是 Spring 无法自动装配任何依赖项的原因。

查看此问题的公认答案: How to make an @WebService spring aware

【讨论】:

  • 谢谢。我不能在我的情况下使用 cxf。我在扩展 SpringBeanAutowiringSupport 时让它工作了。我现在的服务描述给了我另外两个方法 processInjectionBasedOnCurrentContext 和 processInjectionBasedOnServletContext 任何方法来摆脱它
  • 那里接受的答案使用 SpringBeanAutowiringSupport,所以请接受。要解决您的其他问题,请覆盖这些方法(调用 super)并使用 @WebMethod(exclude=true) 进行注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2019-11-12
  • 2015-06-04
  • 2021-11-15
  • 2014-08-31
  • 2016-05-01
相关资源
最近更新 更多