Spring Beans Injection in JAX-WS / Tomcat 7 (@WebServices) - Does not work, returns NullPointerException (NPE)

4

I would like to inject ( @Autowired or @Resource ) beans created by Spring ( 3.x or 4.x ) into a class that implements a JAX-WS interface (reference implementation).

I tested it on Tomcat 6 and it works, but on Tomcat 7, it looks like the container specification has changed and the contexts are different.

I have already used the proposed integration (which even has bad documentation) as suggested in:

In this example, the sun-jaxws.xml file is replaced by a Spring configuration file. However, in my example I'm doing everything with @Configuration , since I was trying to avoid XMLs. I do not know how to make this change to the JAX-WS Endpoint to be able to use a spring-injected bean, without having to use applicationContext.getBean("meuBean") .

Note: I read everything, believe me. Some suggest extending SpringBeanAutowiringSupport , others putting @PostConstructor , but nothing works on Tomcat 7.

Is it a container definition? Can not really do it? This is the main question, for me to give up once and for all. ;)

UPDATE

@utluiz, thanks for the reply. Basically I used the Mkyong example, however I want to do the injection without XML and the configuration through the @Configuration java class I created. Meu web.xml looks like this:

             org.springframework.web.context.ContextLoaderListener     

<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
        instead of the default XmlWebApplicationContext -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
   fully-qualified @Configuration classes. Fully-qualified packages may also be
   specified for component-scanning -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.mkyong.ContextConfiguration</param-value>
</context-param>


<servlet>
    <servlet-name>jaxws-servlet</servlet-name>
    <servlet-class>
        com.sun.xml.ws.transport.http.servlet.WSSpringServlet
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/hello/*</url-pattern>
</servlet-mapping>

and my configuration class:

@Configuration
@ComponentScan(basePackages = "com.mkyong")
@ImportResource({"classpath*:/applicationContext.xml"})
public class ContextConfiguration {

}

However, the endpoint mapping gets in this xml (applicationContext.xml) as follows:

<wss:binding url="/hello">
    <wss:service>
        <ws:service bean="#helloWorldWS"/>
    </wss:service>
</wss:binding>

<!-- Web service methods -->
<bean id="helloWs" class="com.mkyong.ws.HelloWorldWS">
    <property name="helloWorldBo" ref="HelloWorldBo" />
</bean>

<bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl" />

I would like to remove this XML, for this, I need to configure the wss:binding stretch within my class @Configuration in java. This I do not know how to do.

Note: It would be okay to use this XML as well, but I wanted to do the injections via java and not via XML. But it seems that spring ignores the applicationContext.xml when I use the context setting via java.

I do not know if it was clear, the case is that I have no problem with the following snippet being in the XML:

<wss:binding url="/hello">
    <wss:service>
        <ws:service bean="#helloWorldWS"/>
    </wss:service>
</wss:binding>

I just want to remove the bean definitions, so I can use @Resource / @Autowired and @Component .

    
asked by anonymous 09.09.2014 / 20:42

1 answer

2

Save!

I believe I have the answer to your question. I hope it's still relevant.

What happens is that the JAX-WS configuration is not the Spring but the library provided by Java .NET , more specifically this face:

<dependency>
    <groupId>org.jvnet.jax-ws-commons.spring</groupId>
    <artifactId>jaxws-spring</artifactId>
    <version>1.9</version>
</dependency>

And apparently it still does not provide the configuration per class and only by xml . What could be done is to undo the configuration of it and create you the configuration class of definition of beans . Something like a @EnableJAXWS and there provide support for client classes to configure endpoints .

If this is to be done, I suggest starting by taking a look at the jaxws-spring library and understanding the beans creation. The namespace org.jvnet.jax_ws_commons.spring is a good start.

If you do not want to do this, keep the XML and have your decision documented. By coincidence, this question was also asked in the EN version of the site: link . And there (until then) has no answer. :)

    
21.07.2015 / 17:53