开发者

Jax-ws, spring and SpringBeanAutowiringSupport

although in my @Webservice class I extend SpringBeanAutowiringSupport, autowiring sim开发者_StackOverflowply 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?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜