How can I access the ApplicationContext from within a JAX-WS web service?

266

Solution 1

I don't think that the web service should have to know about web or servlet contexts or its application context. I don't see why it should have to know any of that. Shouldn't it be far more passive? Inject what it needs and let it do its work. The service interactions with a client should be based on a contract defined up front. If it has to get unknown values from a context of some kind, how will clients know what needs to be set or how to set it?

I'd go further and say that a web service should be a wrapper for a Spring service interface. It's just one more choice among all the possible ways to expose it. Your web service should do little more than marshal and unmarshal the XML request/response objects and collaborate with Spring services.

Solution 2

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;
  ...
}

Solution 3

Make your web service bean extend a spring bean.

like this

Share:
266
Dan
Author by

Dan

Updated on July 10, 2022

Comments

  • Dan
    Dan almost 2 years

    I need to cancel any previous goroutine on every function call. How would that be handled in Go? I've seen channels used but I can't quite wrap my head around the examples and whether a select statement is necessary.

    The desired result would be that only the last request subtasks are ran.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        for i := 0; i < 5; i++ {
            go handleRequest(i)
            time.Sleep(1 * time.Second) // Time between requests
        }
    }
    
    func handleRequest(incr int) {
        fmt.Println("New request registered: ", incr + 1)
        for i := 0; i <= 3; i++ {
            fmt.Println("Request: ", incr + 1, " | Sub-task: ", i + 1)
            time.Sleep(2 * time.Second) // Time processing
        }
        return
    }
    
  • kkessell
    kkessell about 15 years
    Well, then how can I collaborate with Spring services, if I cannot say: appContext.getBean('myBean')?
  • duffymo
    duffymo about 15 years
    Inject it in via setter or constructor. Dependency injection means "don't call us; we'll call you." Your objects don't have to have the app context to get what they need.
  • duffymo
    duffymo about 15 years
    I'm writing Spring web services that I'm deploying under WebLogic, and I don't have to supply the application context. They're working fine for me - SOAP UI clients have no problems working with them. I think you're doing something else wrong.
  • kkessell
    kkessell about 15 years
    So, you autowired the required beans in your Service class?
  • Matthew T. Staebler
    Matthew T. Staebler about 14 years
    +1 Thanks for pointing me in the direction of SpringBeanAutowiringSupport. I had been struggling with getting Glassfish to relinquish its management of JAX-WS and letting Spring to it. This is a much easier solution and lets Spring stay focused on the things that it is good at.
  • AR3Y35
    AR3Y35 over 11 years
    I've been trying to get the solution to work but SpringBeanAutowiringSupport just doesn't seem to have an effect on my web service. stackoverflow.com/questions/12869014/…
  • dbreaux
    dbreaux about 10 years
    Boy, a simple example of what you mean would be really helpful.
  • Burak Serdar
    Burak Serdar over 4 years
    What kind of an if? select with default is how you do nonblocking read.
  • Dan
    Dan over 4 years
    Is there a way handle the cancellation at a higher level and more immediately? After looking at it, it seems there could be a race condition where in handleRequest if a long process occurs before the select statement. Say a new request hits in main immediately after the select is hit in handleRequest goroutine. The subtask in the previous handleRequest would still process before hitting the select statement again to register the context is done.
  • Burak Serdar
    Burak Serdar over 4 years
    No. If you're worried about other goroutines doing things that may affect the canceled goroutine, then check cancelation multiple times, and every time before you do something that can be affected. There is no other way to cancel a goroutine.
  • Till Kuhn
    Till Kuhn almost 2 years
    Nice & easy, thanks. But it should be go handleRequest(ctx,i) in line 10 (context first), or the code will not compile.