尽管在我的@Webservice 类中我扩展了 SpringBeanAutowiringSupport,但自动装配根本不适用于 Spring 2.5、tomcat6。
没有注入任何东西。
我使用classpathcontext在main方法中测试了这些bean的自动装配,一切都很好。但不适用于 jax-ws 端点。
你有想法吗?
我找到了解决方案。问题是 Spring 不会为@WebService
类自动装配 bean(正如在其他论坛上发现的那样,它可能是当前的错误)。
解决方案:
使用org.springframework.beans.factory.config.AutowireCapableBeanFactory.class
而不是使用@Autowired
注解来注入您的 bean(例如@Service
,@Repository
等)。
所以:
包括@Resource
WebServiceContext
@Resource
private WebServiceContext context;
用它来获取你的 bean
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 中侦听器的顺序对我有用。在 WSServletContextListener 之前放置 ContextLoaderListener 解决了这个问题。
我猜你正在使用这个配置元素:
<context:annotation-config />
但要启用对 @Endpoint 注释的支持,您必须添加此元素:
<context:component-scan base-package="" />
如果您使用一些参考实现会更好,例如 Metro、Axis2、apache-cxf,以便在 Web 服务上轻松配置此类端点。