Question

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?

Was it helpful?

Solution

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="" />

OTHER TIPS

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 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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top