web service exposed by extending SpringBeanAutowiringSupport is failing to inject @Autowired dependencies

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

My web service exposed by extending SpringBeanAutowiringSupport is failing to inject @Autowired dependencies.

Web service deploys fine and I'm able to invoke the @WebMethod but I'm getting a NullPointerException due to the failed injection.

I put System.out.println("Consructing XmlContactMapper..."); in the constructor of XmlContactMapper (one of my dependencies with @Autowired). When I deploy the web service I see the debug line so I know the constructor is being invoked. But for some reason an instance of XmlContactMapper isn't being injected into my ContactServiceImpl xmlMapper property.

Any ideas on what I'm doing wrong?

Using...

  • 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> 

web service

@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; 

Logging

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. 

回答1:

Like @Biji suggested, I think this could be a load-order issue between your ContactServiceImpl vs. ContactComponent

Typically, the reason you would need to extend SpringBeanAutowiringSupport is that you have some bean that's instantiated outside of your Spring container, but you want it to resolve it's dependencies via Spring.

Since you're using Glassfish + Metro, you might take a look at the Metro Spring integration support: http://jax-ws-commons.java.net/spring/

Using this route, the com.sun.xml.ws.transport.http.servlet.WSSpringServlet should take care of proper loading order with respect to your Spring context.



回答2:

The order of the listeners is important. The ContextLoaderListener must be defined before the 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:

As Zeemee stated already, the order of the listener definition is crucial. Additionally I had to add <absolute-ordering/> in web.xml, because Tomcat 8.0.26 gave no respect to the order by itself.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!