我通过扩展 SpringBeanAutowiringSupport 公开的 Web 服务无法注入 @Autowired 依赖项。
Web 服务部署正常,我能够调用 @WebMethod,但由于注入失败,我收到 NullPointerException。
我放入System.out.println("Consructing XmlContactMapper...");了 XmlContactMapper 的构造函数(我对 @Autowired 的依赖项之一)。当我部署 Web 服务时,我看到调试行,因此我知道正在调用构造函数。但由于某种原因,XmlContactMapper 的实例没有被注入到我的 ContactServiceImpl xmlMapper 属性中。
关于我做错了什么有什么想法吗?
使用...
<?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>
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
@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;
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
小智 2
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.
由于您使用的是 Glassfish + Metro,因此您可以查看 Metro Spring 集成支持: http://jax-ws-commons.java.net/spring/
使用此路由,com.sun.xml.ws.transport.http.servlet.WSSpringServlet应该根据 Spring 上下文处理正确的加载顺序。
| 归档时间: |
|
| 查看次数: |
9543 次 |
| 最近记录: |