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
.
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.
– utluiz
Thanks utluiz, I edited my question.
– Igor Veloso