儘管在我的@Webservice類中 我擴展了SpringBeanAutowiringSupport,自動裝配根本不適用於Spring 2.5, tomcat6。Jax-ws,Spring和SpringBeanAutowiringSupport
什麼都沒有注入。
我在main方法中測試了那些自動裝配的bean,使用classpathcontext,一切都被注入正常。 但不適用於jax-ws端點。
你有什麼想法嗎?
儘管在我的@Webservice類中 我擴展了SpringBeanAutowiringSupport,自動裝配根本不適用於Spring 2.5, tomcat6。Jax-ws,Spring和SpringBeanAutowiringSupport
什麼都沒有注入。
我在main方法中測試了那些自動裝配的bean,使用classpathcontext,一切都被注入正常。 但不適用於jax-ws端點。
你有什麼想法嗎?
我猜你使用這個配置元素:
<context:annotation-config />
但以啓用@Endpoint註解支持,你必須添加這個元素:
<context:component-scan base-package="" />
我已經找到了解決方案。問題在於Spring沒有爲@WebService
類自動調用bean(如在其他論壇上發現的,它可能是當前的錯誤)。
解決方案:
使用org.springframework.beans.factory.config.AutowireCapableBeanFactory.class
,而不是使用@Autowired
註解注入你的bean(例如@Service
,@Repository
等)。
所以:
包括@Resource
WebServiceContext
@Resource
private WebServiceContext context;
用它讓你的豆
MyDAO myDAO = null;
ServletContext servletContext = (ServletContext) context
.getMessageContext().get("javax.xml.ws.servlet.context");
WebApplicationContext webApplicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
myDAO = (MyDAO) webApplicationContext
.getAutowireCapableBeanFactory().getBean("myDAO");
MyDAO
類可以如下:
@Service
@Qualifier("myDAO")
@Transactional
public class MyDAO {
private HibernateTemplate hibernateTemplate;
@Required
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public MyInfo getMyInfo(Long id){
return this.hibernateTemplate.get(MyInfo.class, id);
}
//...
}
此後您可以使用myDAO
對象的@WebMethod
方法。
我不知道它是否與其他人一樣。它通過改變web.xml中監聽器的順序來爲我工作。將ContextLoaderListener放在WSServletContextListener之前解決問題。
如果您使用某些參考實現(如Metro,Axis2,apache-cxf)以便在Web服務上輕鬆配置此類端點,那將會更好。
我在說「jaxws:endpoint」xml元素。正如你所提到的那樣,我使用了組件掃描,但使用了非空的base-package,即base-package =「pack1,pack2」。仍然不工作:) – EugeneP 2010-05-31 12:16:43
我有同樣的問題。我在這個答案中提到了配置。我正在使用GlassFish 3.1和Spring 3.0.5.RELEASE。還有其他建議嗎? – AR3Y35 2012-10-18 18:37:44