Similar a How can I access the ServletContext from within a JAX-WS web service?, ¿hay alguna manera de acceder a applicationContext, más fácil que esto?¿Cómo puedo acceder al ApplicationContext desde un servicio web JAX-WS?
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
@WebService
public class MyWebService {
// boilerplate code begins :(
@Resource
private WebServiceContext context;
private WebApplicationContext webApplicationContext = null;
/**
* @return
* @throws IllegalStateException
*/
private WebApplicationContext getWebApplicationContext()
throws IllegalStateException {
if (webApplicationContext != null)
return webApplicationContext;
ServletContext servletContext =
(ServletContext) context.getMessageContext().get(
MessageContext.SERVLET_CONTEXT);
webApplicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
return webApplicationContext;
}
}
Bueno, entonces ¿cómo puedo colaborar con los servicios de Spring, si no puedo decir: appContext.getBean ('myBean')? – pihentagy
Inyectarlo a través de setter o constructor. La inyección de dependencia significa "no nos llame, lo llamaremos". Sus objetos no tienen que tener el contexto de la aplicación para obtener lo que necesitan. – duffymo
No puedes. Si pruebo mi servicio web en glassfish, se crea un nuevo servicio web, y no está configurado: -o Eso fue una depuración de un día para obtener este conocimiento :( – pihentagy