java Jax-ws、spring 和 SpringBeanAutowiringSupport

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2943082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:34:54  来源:igfitidea点击:

Jax-ws, spring and SpringBeanAutowiringSupport

javaspringjax-wscxfautowired

提问by EugeneP

although in my @Webservice class I extend SpringBeanAutowiringSupport, autowiring simply does not work for Spring 2.5, tomcat6.

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

nothing is injected.

什么都没有注入。

I tested those beans autowiring in main method, using classpathcontext, everything is injected fine. But not for jax-ws endpoint.

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

do you have ideas?

你有想法吗?

采纳答案by Espen

I'm guessing that you're using this config element:

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

<context:annotation-config />

But to enable support for the @Endpoint annotation, you must add this element:

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

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

回答by JuRezz

I've found the solution. The problem is that Spring doesn't autowire beans for @WebServiceclasses (as found on other forums it might be a current bug).

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

The solution:

解决办法

Use org.springframework.beans.factory.config.AutowireCapableBeanFactory.classinstead of using @Autowiredannotation for injecting your beans (e.g. @Service, @Repositoryetc).

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

So:

所以:

  1. include @ResourceWebServiceContext

    @Resource
    private WebServiceContext context;  
    
  2. use it for getting your 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");
    

    MyDAOclass can be as follows:

    @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. after this you can use myDAOobject in the @WebMethodmethod.

  1. 包括 @ResourceWebServiceContext

    @Resource
    private WebServiceContext context;  
    
  2. 用它来获取你的豆子

    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方法中使用对象。

回答by JimTux

I don't know if it's the same case as everyone else. It worked for me by changing the order of the listeners in web.xml. Putting the ContextLoaderListener before WSServletContextListener resolved the issue.

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

回答by puru

It would be better if you used some reference implementation, like Metro, Axis2, apache-cxf for easy configuration of such endpoint on web service.

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