Hello,
my intention is to inject a Bean with the @Autowire annotation to a JAX-WS Annotiated class.
This is working for a normal pring WS approach (@Controller / @RequestMapping) but not for the JAX-WS part.
My container is a Tomcat 6 instance and Spring 3.0.5.
My code is the following.
Bean:
Appconfig:Code:package empty.beans; public class MyBean { private String name = "init"; private int counter = 0; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCounter() { return counter; } public void setCounter(int counter) { this.counter = counter; } }
JAX-WS:Code:package empty.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import empty.beans.MyBean; @Configuration public class AppConfig { MyBean myBean; public @Bean MyBean myBean() { if (myBean == null) { myBean = new MyBean(); } return myBean; } }
Returns me every time that myBean is null.Code:package empty.ws; import javax.jws.WebMethod; import javax.jws.WebResult; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import empty.beans.MyBean; @WebService(serviceName = "MyService", portName = "MyPort") public class TestWS { @Autowired MyBean myBean; @WebMethod(operationName="testBean") @WebResult(name = "ret") public String xxx() { return "yeah, myBean != null " + (myBean != null); } }
A Spring WS:
Returns 1,2,3,4,5,.....Code:package empty.ws; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import empty.beans.MyBean; @Controller @RequestMapping(value="/springws") public class SpringWS { @Autowired MyBean myBean; @RequestMapping(method=RequestMethod.GET) public @ResponseBody String getAllParameter() { myBean.setCounter(myBean.getCounter()+1); return String.valueOf(myBean.getCounter()); } }
web.xml:
Code:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>--------</display-name> <listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <!-- Startup listener for Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Dispatcher servlet for Spring --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>110</load-on-startup> </servlet> <servlet> <servlet-name>test-ws</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>test-ws</servlet-name> <url-pattern>/testws</url-pattern> </servlet-mapping> </web-app>
ApplicationContext.xml:
Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="empty" /> <context:annotation-config /> <mvc:annotation-driven /> </beans>
thanks in advance.


Reply With Quote
