Jax-ws, spring and SpringBeanAutowiringSupport

15,802

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
15,802
EugeneP
Author by

EugeneP

I am a Java developer. Feel free to send me a letter Need some help from experienced developers on personal growth and mastering Java. Would love to hear from you. Eugene [email protected]

Updated on June 04, 2022

Comments

  • EugeneP
    EugeneP almost 2 years

    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?