|
|
JAX-WS class doesn't autowire my Bean
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:
Code:
package empty.beans;
public class MyBean {
private String name = quot;initquot;;
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;
}
}
Appconfig:
Code:
package empty.configuration;
import org..context.annotation.Bean;
import org..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;
}
}
JAX-WS:
Code:
package empty.ws;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import org..beans.factory.annotation.Autowired;
import empty.beans.MyBean;
@WebService(serviceName = quot;MyServicequot;, portName = quot;MyPortquot;)
public class TestWS {
@Autowired
MyBean myBean;
@WebMethod(operationName=quot;testBeanquot;)
@WebResult(name = quot;retquot;)
public String xxx() {
return quot;yeah, myBean != null quot; + (myBean != null);
}
}
Returns me every time that myBean is null.
A Spring WS:
Code:
package empty.ws;
import org..beans.factory.annotation.Autowired;
import org..stereotype.Controller;
import org..web.bind.annotation.RequestMapping;
import org..web.bind.annotation.RequestMethod;
import org..web.bind.annotation.ResponseBody;
import empty.beans.MyBean;
@Controller
@RequestMapping(value=quot;/springwsquot;)
public class SpringWS {
@Autowired
MyBean myBean;
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String getAllParameter() {
myBean.setCounter(myBean.getCounter()+1);
return String.valueOf(myBean.getCounter());
}
}
Returns 1,2,3,4,5,.....
web.xml:
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;web-app xmlns=quot;xml/ns/j2eequot; xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/j2ee xml/ns/j2ee/web-app_2_4.xsdquot;
version=quot;2.4quot;gt;
lt;display-namegt;--------lt;/display-namegt;
lt;listenergt;
lt;listener-classgt;com.sun.xml.ws.transport.from.servlet.WSServletContextListenerlt;/listener-classgt;
lt;/listenergt;
lt;!-- Startup listener for Spring --gt;
lt;listenergt;
lt;listener-classgt;org..web.context.ContextLoaderListenerlt;/listener-classgt;
lt;/listenergt;
lt;!-- Dispatcher servlet for Spring --gt;
lt;servletgt;
lt;servlet-namegt;dispatcherlt;/servlet-namegt;
lt;servlet-classgt;org..web.servlet.DispatcherServletlt;/servlet-classgt;
lt;init-paramgt;
lt;param-namegt;contextConfigLocationlt;/param-namegt;
lt;param-valuegt;/WEB-INF/applicationContext.xmllt;/param-valuegt;
lt;/init-paramgt;
lt;load-on-startupgt;110lt;/load-on-startupgt;
lt;/servletgt;lt;servletgt;
lt;servlet-namegt;test-wslt;/servlet-namegt;
lt;servlet-classgt;com.sun.xml.ws.transport.from.servlet.WSServletlt;/servlet-classgt;
lt;load-on-startupgt;1lt;/load-on-startupgt;
lt;/servletgt;
lt;servlet-mappinggt;
lt;servlet-namegt;dispatcherlt;/servlet-namegt;
lt;ucl-patterngt;/api/*lt;/ucl-patterngt;
lt;/servlet-mappinggt;
lt;servlet-mappinggt;
lt;servlet-namegt;test-wslt;/servlet-namegt;
lt;ucl-patterngt;/testwslt;/ucl-patterngt;
lt;/servlet-mappinggt;
lt;/web-appgt;ApplicationContext.xml:
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:context=quot;schema/contextquot;
xmlns:mvc=quot;schema/mvcquot;
xsi:schemaLocation=quot;schema/beans schema/beans/spring-beans.xsd schema/mvc schema/mvc/spring-mvc-3.0.xsd schema/context schema/context/spring-context-3.0.xsdquot;gt;
lt;context:component-scan base-package=quot;emptyquot; /gt;
lt;context:annotation-config /gt;
lt;mvc:annotation-driven /gt;
lt;/beansgt;
thanks in advance.
Where is the annotation telling Spring to dig into this classe on the class TestWS ?
Putting it in the package scanned by Spring with lt;context:component-scan base-package=quot;emptyquot; /gt; is not enough.
Look at your controller SpringWs, it is working because it have on top of it the annotation @Controller.
This annotation is telling to Spring : quot;im a bean ,precisely a controller so please dig into my code to found more (@Autowired...)quot;
So add to your classe TestWs either @Repository or @Service
And verify that you are not missing some jar dependency
To learn more : blog/2007/11...uration-spring
lt;/idgt;
lt;namegt;Java.net Repository for Maven 2lt;/namegt;
lt;uclgt;maven/2/lt;/uclgt;
lt;/repositorygt;
lt;/repositoriesgt;lt;dependenciesgt;
lt;dependencygt;
lt;groupIdgt;javax.servletlt;/groupIdgt;
lt;artifactIdgt;servlet-apilt;/artifactIdgt;
lt;versiongt;2.4lt;/versiongt;
lt;scopegt;providedlt;/scopegt;
lt;/dependencygt;
lt;dependencygt;
lt;groupIdgt;com.sun.xml.wslt;/groupIdgt;
lt;artifactIdgt;jaxws-rtlt;/artifactIdgt;
lt;versiongt;2.2.1lt;/versiongt;
lt;/dependencygt;
lt;dependencygt;
lt;groupIdgt;log4jlt;/groupIdgt;
lt;artifactIdgt;log4jlt;/artifactIdgt;
lt;versiongt;1.2.8lt;/versiongt;
lt;typegt;jarlt;/typegt;
lt;/dependencygt;
lt;dependencygt;
lt;groupIdgt;org.lt;/groupIdgt;
lt;artifactIdgt;org..web.servletlt;/artifactIdgt;
lt;versiongt;3.0.5.RELEASElt;/versiongt;
lt;/dependencygt;
lt;dependencygt;
lt;groupIdgt;commons-logginglt;/groupIdgt;
lt;artifactIdgt;commons-logginglt;/artifactIdgt;
lt;versiongt;1.1lt;/versiongt;
lt;/dependencygt;
lt;dependencygt;
lt;groupIdgt;cgliblt;/groupIdgt;
lt;artifactIdgt;cglib-nodeplt;/artifactIdgt;
lt;versiongt;2.2lt;/versiongt;
lt;/dependencygt;
lt;/dependenciesgt;
lt;!-- Spring --gt;
lt;buildgt;
lt;finalNamegt;testwslt;/finalNamegt;
lt;pluginsgt;
lt;plugingt;
lt;groupIdgt;org.apache.maven.pluginslt;/groupIdgt;
lt;artifactIdgt;maven-eclipse-pluginlt;/artifactIdgt;
lt;configurationgt;
lt;wtpversiongt;2.0lt;/wtpversiongt;
lt;/configurationgt;
lt;/plugingt;
lt;plugingt;
lt;groupIdgt;org.apache.maven.pluginslt;/groupIdgt;
lt;artifactIdgt;maven-compiler-pluginlt;/artifactIdgt;
lt;versiongt;2.1lt;/versiongt;
lt;configurationgt;
lt;sourcegt;1.6lt;/sourcegt;
lt;targetgt;1.6lt;/targetgt;
lt;/configurationgt;
lt;/plugingt;
lt;/pluginsgt;
lt;/buildgt;
lt;pluginRepositoriesgt;
lt;pluginRepositorygt;
lt;idgt;maven2-repository.dev.java.netlt;/idgt;
lt;uclgt;maven/2/lt;/uclgt;
lt;/pluginRepositorygt;
lt;/pluginRepositoriesgt;
lt;/projectgt;And where is spring-context jar amp; eventually spring-core?
Try with spring-context amp; if it is not working, add spring-core but im almost certain that you are missing spring-context because this contains class like :
ClassPathScanningCandidateComponentProvider (A component provider that scans the classpath from a base package).
lt;dependencygt; lt;groupIdgt;org.lt;/groupIdgt; lt;artifactIdgt;spring-corelt;/artifactIdgt; lt;versiongt;${spring.version}lt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.lt;/groupIdgt; lt;artifactIdgt;spring-contextlt;/artifactIdgt; lt;versiongt;${spring.version}lt;/versiongt; lt;/dependencygt;
Although including this libs seperatly does not help.
If I deploy the war in JBoss 6 instead of Tomcat 6 I got:Code:
14:29:32,060 INFO [STDOUT] 2011-01-17 14:29:32,059 DEBUG: SpringBeanAutowiringSupport - Current WebApplicationContext is notavailable for processing of TestWS: Make sure this class gets constructed in a Spring web application. Proceeding without in jection.
Anyway, there is a closed bug ticket for Weblogic server that say Injection in JAX-WS isn't working.
Is here someone that used autowiring support in JAX-WS project on an actual Tomcat?
Doesn't work in Glassfish either.
Had my Tomcat logger wronmg configured, having this error on the log also:Code:
DEBUG: org..web.context.support.SpringBeanAutowiringSupport [main] - [89] Current WebApplicationContext is not available for processing of TestWS: Make sure this class gets constructed in a Spring web application. Proceeding without injection.
What do I wrong?
Originally Posted by backToMoonHad my Tomcat logger wronmg configured, having this error on the log also:Code:
DEBUG: org..web.context.support.SpringBeanAutowiringSupport [main] - [89] Current WebApplicationContext is not available for processing of TestWS: Make sure this class gets constructed in a Spring web application. Proceeding without injection.
What do I wrong?
This error message dissoccurs if I remove SpringBeanAutowiringSupport from the extend list of the TestWS class.
Post your project here (the minimal version with the problem) (rapidhsare, megaupload or whatever)
im gonna debug it. Yeah it is E-christhmas |
|