【问题标题】:web service exposed by extending SpringBeanAutowiringSupport is failing to inject @Autowired dependencies通过扩展 SpringBeanAutowiringSupport 暴露的 Web 服务无法注入 @Autowired 依赖项
【发布时间】:2012-10-13 01:02:55
【问题描述】:

我通过扩展 SpringBeanAutowiringSupport 暴露的 Web 服务无法注入 @Autowired 依赖项。

Web 服务部署良好,我可以调用 @WebMethod,但由于注入失败,我得到了 NullPointerException。

我将System.out.println("Consructing XmlContactMapper..."); 放在 XmlContactMapper 的构造函数中(我与 @Autowired 的依赖项之一)。当我部署 Web 服务时,我看到了调试行,因此我知道正在调用构造函数。但由于某种原因,XmlContactMapper 的实例没有被注入到我的 ContactServiceImpl xmlMapper 属性中。

关于我做错了什么有什么想法吗?

使用...

  • Spring 3.0.5.RELEASE
  • GlassFish 3.1
  • jaxws-rt 2.1.4

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0" metadata-complete="true">

    <display-name>contact-sib</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>           
            classpath:/config/bean-config.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>JaxWsEndpoint</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JaxWsEndpoint</servlet-name>
        <url-pattern>/services/contact</url-pattern>
    </servlet-mapping>


</web-app>

sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
           version="2.0">

  <endpoint name="ContactService" 
            implementation="com.bb.sc.sib.contact.ContactServiceImpl" 
            url-pattern="/services/contact"/>

</endpoints>

bean-config.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"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xmlns:p = "http://www.springframework.org/schema/p"
       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
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.bb.sc.sib.contact"/>
    <context:annotation-config/>

    <bean id="xmlMapper" class="com.bb.sc.sib.contact.XmlContactMapper"/>

</beans>

网络服务

@WebService (endpointInterface="com.bb.sc.sei.contact.ContactService", serviceName="JaxWsContactService")
public class ContactServiceImpl extends SpringBeanAutowiringSupport implements ContactService {
    @Autowired
    private ContactComponent contactComponent;
    @Autowired
    private MapperFacade xmlMapper;

记录

INFO: 10:56:40.073 [admin-thread-pool-4848(419)] DEBUG o.s.w.c.s.SpringBeanAutowiringSupport - Current WebApplicationContext is not available for processing of ContactServiceImpl: Make sure this class gets constructed in a Spring web application. Proceeding without injection.

【问题讨论】:

  • 有趣,我不知道您实际上可以扩展 SpringBeanAutowiringSupport 并连接字段 - 我可以建议打开 DEBUG 级别并查看 Spring 在这里打印的内容,这可以提供一个很好的方向出错了
  • 听起来你的 web 服务类 ContactServiceImpl 在 Spring 容器加载之前就被实例化了。
  • 当我部署 Web 服务时,我确实首先在我的 ContactServiceImpl 中看到了调试行。所以这个问题似乎与时间有关。
  • ContactServiceImpl bean 是否在您的 spring 上下文中定义?
  • ContactServiceImpl 未在我的 spring 上下文(bean-config)中定义。我确实尝试在那里定义它并且注入工作正常,但该实例不是从 /services/contact servlet URL 调用的。问题似乎是我的端点是在我的 spring 上下文之前初始化的。所以 SpringBeanAutowringSupport 没有任何作用。我已经更新了我的帖子以包含一个提到这一点的调试日志。

标签: spring


【解决方案1】:

就像@Biji 建议的那样,我认为这可能是您的 ContactServiceImpl 与 ContactComponent 之间的加载顺序问题

通常,您需要扩展 SpringBeanAutowiringSupport 的原因是您有一些在 Spring 容器之外实例化的 bean,但您希望它通过 Spring 解决它的依赖关系。

由于您使用的是 Glassfish + Metro,因此您可以查看 Metro Spring 集成支持: http://jax-ws-commons.java.net/spring/

使用此路由,com.sun.xml.ws.transport.http.servlet.WSSpringServlet 应该根据您的 Spring 上下文处理正确的加载顺序。

【讨论】:

  • 用 WSSpringServlet 替换 WSServlet 有效。我摆脱了 SpringBeanAutowiringSupport 扩展和 sun-jaxws.xml。我像往常一样在 bean-congif.xml 中配置了一个 ContactServiceImpl bean。然后我将该 bean 连接到 jaxws servlet 并按照 示例公开该服务。谢谢
【解决方案2】:

听众的顺序很重要。 ContextLoaderListener 必须在 WSServletContextListener 之前定义。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:wsContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

【讨论】:

    【解决方案3】:

    正如 Zeemee 已经说过的,监听器定义的顺序是至关重要的。此外,我不得不在web.xml 中添加&lt;absolute-ordering/&gt;,因为Tomcat 8.0.26 本身不考虑顺序。

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      相关资源
      最近更新 更多