3

尽管在我的@Webservice 类中我扩展了 SpringBeanAutowiringSupport,但自动装配根本不适用于 Spring 2.5、tomcat6。

没有注入任何东西。

我使用classpathcontext在main方法中测试了这些bean的自动装配,一切都很好。但不适用于 jax-ws 端点。

你有想法吗?

4

4 回答 4

9

我找到了解决方案。问题是 Spring 不会为@WebService类自动装配 bean(正如在其他论坛上发现的那样,它可能是当前的错误)。

解决方案

使用org.springframework.beans.factory.config.AutowireCapableBeanFactory.class而不是使用@Autowired注解来注入您的 bean(例如@Service@Repository等)。

所以:

  1. 包括@Resource WebServiceContext

    @Resource
    private WebServiceContext context;  
    
  2. 用它来获取你的 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);
        }
    
        //...
    }
    
  3. 在此之后,您可以在方法中使用myDAO对象。@WebMethod

于 2010-11-24T14:20:59.270 回答
4

不知道是不是和大家一样。它通过更改 web.xml 中侦听器的顺序对我有用。在 WSServletContextListener 之前放置 ContextLoaderListener 解决了这个问题。

于 2012-08-01T13:01:14.163 回答
1

我猜你正在使用这个配置元素:

<context:annotation-config />

但要启用对 @Endpoint 注释的支持,您必须添加此元素:

<context:component-scan base-package="" />
于 2010-05-31T11:24:16.110 回答
0

如果您使用一些参考实现会更好,例如 Metro、Axis2、apache-cxf,以便在 Web 服务上轻松配置此类端点。

于 2014-02-11T12:03:40.263 回答