Injection of Beans Spring into JAX-WS / Tomcat 7 (@Webservices) - Does not work, returns Nullpointerexception (NPE)

Asked

Viewed 554 times

4

I’d like to inject (@Autowired or @Resource) Beans created by Spring (3.x or 4.x) in a class that implements a JAX-WS interface (reference implementation).

I tested on Tomcat 6 and it works, but on Tomcat 7, it seems that 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: http://www.mkyong.com/webservices/jax-ws/jax-ws-spring-integration-example/

In this example, the file sun-jaxws.xml is replaced by a Spring configuration file. However, in my example I am doing everything with @Configuration, 'cause I was trying to avoid Xmls. I don’t know how to make this move to the JAX-WS Endpoint to be able to use a bean injected by Spring without using the applicationContext.getBean("meuBean").

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

Is it a container definition? Is there no way to do it? This is the main question, for me to give up for good. ;)


UPDATE

@utluiz, thanks for the answer. Basically I used the example of Mkyong, but I want to do the injection without XML and the configuration through the java class @Configuration that I created. Meu web.xml was 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, endpoint mapping is 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 in java, the snippet wss:binding within my class @Configuration. This I don’t know how to do.

Note: It would not be a problem to use this XML too, but wanted to make the injections via java and not via XML. But it seems that spring ignores the applicationContext.xml when using context configuration via java.

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

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

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

  • 1

    Igor, welcome to [en.so]! You’d better post your setup. The question is: or the bean is managed by Spring or not. But Tomcat 7 must be calling directly its classes, without going through Spring, so nothing will work anyway. I don’t know if anyone can help without seeing in your project how classes are created.

  • 1

    Thanks utluiz, I edited my question.

1 answer

2


Hail!

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

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

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

And it seems that it still does not provide the configuration per class but only by xml. What could be done is decupate the configuration of it and create you the configuration class of setting beans. Something like a @EnableJAXWS and there provide support for customer classes to configure the endpoints.

If you’re gonna do it, I suggest you start by taking a look at the library jaxws-spring and understand the creation of beans. The namespace org.jvnet.jax_ws_commons.spring is a good start.

If you don’t want to do it, stick to XML and leave your decision documented. By a coincidence, this question was also asked in the EN version of the site: https://stackoverflow.com/questions/14538495/how-to-set-up-a-spring-web-service-without-using-any-xml-files. And there (until then) has no answer. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.