【问题标题】:Jax-ws, spring and SpringBeanAutowiringSupportJax-ws、spring 和 SpringBeanAutowiringSupport
【发布时间】:2011-02-25 22:48:46
【问题描述】:

虽然在我的@Webservice 课程中 我扩展了 SpringBeanAutowiringSupport,自动装配根本不适用于 Spring 2.5, tomcat6.

没有注入任何东西。

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

你有什么想法吗?

【问题讨论】:

    标签: java spring jax-ws cxf autowired


    【解决方案1】:

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

    解决方案

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

    所以:

    1. 包括@ResourceWebServiceContext

      @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. 之后,您可以在@WebMethod 方法中使用myDAO 对象。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        <context:annotation-config />
        

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

        <context:component-scan base-package="" />
        

        【讨论】:

        • 我说的是“jaxws:endpoint”xml 元素。正如您所提到的,我使用组件扫描,但使用非空基本包,即 base-package="pack1,pack2"。还是不行:)
        • 我遇到了同样的问题。我在这个答案中提到了配置。我正在使用 GlassFish 3.1 和 Spring 3.0.5.RELEASE。还有其他建议吗?
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2011-07-26
          • 1970-01-01
          • 1970-01-01
          • 2013-01-13
          • 2011-06-29
          • 2011-12-16
          • 1970-01-01
          • 2011-07-18
          • 1970-01-01
          相关资源
          最近更新 更多