dissabte, 9 de juliol del 2011

Injecting Spring beans in web service handlers

Whenever you have a handler defined for a web service (follow this link for an example) you'll propably find out that any Spring bean you try to inject in your handler results in being null...
It is just beacuse the responsible for instantiating the handler is the underlying web service technology (p.e.: JAX-WS, Axis,...), and that technology is not aware of your Spring context.

Of course there are some workarounds for this problem. 
One approach, could be to:
  1. define the handler class as a singleton Spring bean
  2. inject all the services you need
  3. make sure all those services are thread-safe
  4. make the variables on the handler class, that will store those, services static
This way, when the application starts, the Spring context will be loaded, the "handler bean" created and the service injected. Whenever the handler needs to be instantiated by, for example, JAX-WS, it will have the service injected already (since the variable is static), so you'll be able to use it...

However, keep in minf the cons for this approach: you'll probably end up with a phantom singleton bean in your heap that will never be used.

Cap comentari:

Publica un comentari a l'entrada