Jax-ws、spring 和 SpringBeanAutowiringSupport

发布于 2024-09-03 18:30:05 字数 198 浏览 7 评论 0原文

虽然在我的@Webservice类中 我扩展了 SpringBeanAutowiringSupport,自动装配根本不适用于 Spring 2.5, 汤姆猫6。

没有注入任何东西。

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

你有想法吗?

although in my @Webservice class
I extend SpringBeanAutowiringSupport, autowiring simply does not work for 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.

do you have ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情旳空白 2024-09-10 18:30:05

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

解决方案

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

所以:

  1. 包含 @Resource WebServiceContext

    <前><代码>@Resource
    私有WebServiceContext上下文;

  2. 用它来获取你的bean

    MyDAO myDAO = null;
    ServletContext servletContext = (ServletContext) 上下文
        .getMessageContext().get("javax.xml.ws.servlet.context");
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
        .getRequiredWebApplicationContext(servletContext);
    myDAO = (MyDAO) webApplicationContext
        .getAutowireCapableBeanFactory().getBean("myDAO");
    

    MyDAO 类可以如下所示:

    <前><代码>@Service
    @Qualifier(“myDAO”)
    @事务性
    公共类 MyDAO {
    私有 HibernateTemplate hibernateTemplate;

    @必需的
    @Autowired
    公共无效setSessionFactory(SessionFactory sessionFactory){
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    公共MyInfo getMyInfo(长id){
    返回 this.hibernateTemplate.get(MyInfo.class, id);
    }

    //...
    }

  3. 在此之后,您可以在 中使用 myDAO 对象@WebMethod 方法。

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

The solution:

Use org.springframework.beans.factory.config.AutowireCapableBeanFactory.class instead of using @Autowired annotation for injecting your beans (e.g. @Service, @Repository etc).

So:

  1. include @Resource WebServiceContext

    @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");
    

    MyDAO class 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 myDAO object in the @WebMethod method.

森末i 2024-09-10 18:30:05

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

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.

心的位置 2024-09-10 18:30:05

我猜您正在使用此配置元素:

<context:annotation-config />

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

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

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:

<context:component-scan base-package="" />
音盲 2024-09-10 18:30:05

如果您使用一些参考实现(例如 Metro、Axis2、apache-cxf)来轻松配置 Web 服务上的此类端点,那就更好了。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文