How can I access the ApplicationContext from within a JAX-WS web service?
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
Dan
Updated on July 10, 2022Comments
-
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 about 15 yearsWell, then how can I collaborate with Spring services, if I cannot say: appContext.getBean('myBean')?
-
duffymo about 15 yearsInject 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 about 15 yearsI'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 about 15 yearsSo, you autowired the required beans in your Service class?
-
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 over 11 yearsI'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 about 10 yearsBoy, a simple example of what you mean would be really helpful.
-
Burak Serdar over 4 yearsWhat kind of an
if
?select
with default is how you do nonblocking read. -
Dan over 4 yearsIs 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 inmain
immediately after theselect
is hit inhandleRequest
goroutine. The subtask in the previoushandleRequest
would still process before hitting the select statement again to register the context is done. -
Burak Serdar over 4 yearsNo. 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 almost 2 yearsNice & easy, thanks. But it should be
go handleRequest(ctx,i)
in line 10 (context first), or the code will not compile.