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:
include
@Resource
WebServiceContext
@Resource private WebServiceContext context;
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); } //... }
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.
精彩评论