SpringBeanAutowiringSupport vs WebApplicationContextUtils

5.3k views Asked by At

This is in context to a web application. My Impl class has JAX-WS annotations and @Autowired annotation is not working. I get null pointer exception while retrieving the Autowired objects.

Found to solutions, both work:

  1. Extend SpringBeanAutowiringSupport and keep @Autowired annotations intact.
  2. Use WebApplicationContextUtils to get the WebapplicationContext where beans are loaded. getBean() from this context given me the required bean.

Now, the documentation of SpringBeanAutowiringSupport class says in NOTE "If there is an explicit way to access the ServletContext, prefer such a way over using this class. The WebApplicationContextUtils class allows for easy access to the Spring root web application context based on the ServletContext."

What does this mean? In my web application, I am not using any servlets. My web application is also not an ONLY JAX-WS application. It is a web application with SOAP/REST webservices and EJBs as well.

Which method to prefer and why? Please help.

2

There are 2 answers

1
Paul John On

Extending SpringBeanAutoWiringSupport is mentioned as the preffered way in the spring docs. Isn't this method also better since you wouldn't have to explicitly access the beans from the webcontext?

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#remoting-web-services

0
Peter On

One nice example of use is when you have a web service and you wish to autowire many other spring beans that you made. This looks more elegant than using Context Utils.

@WebService(targetNamespace = "http://yada/yada", endpointInterface = "com.ISomeWS", serviceName = "SomeWS") 
public class SomeWS extends SpringBeanAutowiringSupport implements ISomeWS {
    @Autowired ISpringService springService;
    public WSImportResponse processXMLContent(SomeType xmlContent) throws Exception {
        springService.say("Hello");
    }
}