Domanda

Simile a Come posso accedere a ServletContext da un servizio Web JAX-WS? , c'è un modo per accedere a applicationContext, più facile di così?

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;
    }
}
È stato utile?

Soluzione

Non penso che il servizio web debba conoscere i contesti web o servlet o il suo contesto applicativo. Non vedo perché dovrebbe sapere nulla di tutto ciò. Non dovrebbe essere molto più passivo? Iniettare ciò di cui ha bisogno e lasciarlo fare il suo lavoro. Le interazioni del servizio con un cliente devono essere basate su un contratto definito in anticipo. Se deve ottenere valori sconosciuti da un contesto di qualche tipo, come faranno i clienti a sapere cosa deve essere impostato o come impostarlo?

Andrei oltre e direi che un servizio web dovrebbe essere un wrapper per un'interfaccia di servizio Spring. È solo un'altra scelta tra tutti i modi possibili per esporla. Il tuo servizio web dovrebbe fare poco più che eseguire il marshalling e annullare la gestione degli oggetti richiesta / risposta XML e collaborare con i servizi Spring.

Altri suggerimenti

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;


@WebService( 
    endpointInterface = "Bla", 
    targetNamespace = "http://bla/v001", 
    wsdlLocation = "WEB-INF/wsdl/bla.wsdl",    
    serviceName = "BlaService",
    portName = "BlaPort")
public class BlaWs extends SpringBeanAutowiringSupport implements BlaPort {

  @Autowired
  @Qualifier("dao") 
  private Dao dao;
  ...
}

Rendi il tuo bean di servizio Web estendere un bean di primavera.

come questo

Vorrei installare un filtro che salva ServletContext prima di concatenare un ThreadLocal

Secondo JavaDoc per la classe SpringBeanAutowiringSupport, vedere: http : //docs.spring.io/autorepo/docs/spring-framework/3.0.x/api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

Leggi la NOTA: alla fine di javadoc.

La domanda originale, in realtà, potrebbe essere il modo in cui questa dovrebbe essere implementata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top