JSF annotations don't work with Spring-boot - spring

I had been tried to use info from Spring Boot and JSF/Primefaces/Richfaces, but for me it doesn't work.
I use Java 8, maven, Spring-boot and JSF with PrimeFaces.
I would like to have executable jar and run my application via main method or from command line java -jar myApp.jar.
The problem - JSF-annotations (#ManagedBean, #ManagedProperty) are ignored.
Pom file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.7</version>
</dependency>
...
</dependencies>
I also have tried to add/remove javax.el-api/javax.el/jstl - the same result. For bean initialization I have added section to faces-config.xml
When I change spring-boot-starter-web to spring-boot-starter and have spring-web (according to solution from mentioned post from Herick) I got
java.io.FileNotFoundException: class path resource
[org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class]
cannot be opened because it does not exist
My config class:
#Configuration
#EnableAutoConfiguration//(exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class})
#ComponentScan("hello")
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
}
#Bean
public FacesServlet facesServlet() {
return new FacesServlet();
}
#Bean
public ServletRegistrationBean facesServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), "*.xhtml");
registration.setName("facesServlet");
return registration;
}
#Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}
}
With (exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class}) web.xml configuration doesn't work.
In mentioned post was:
#Bean
public ListenerRegistationBean jsfConfigureListener() {
return new ListenerRegistrationBean(new ConfigureListener());
}
ListenerRegistationBean is absent in my spring-boot and I have used ServletListenerRegistrationBean instead.
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Test</display-name>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<location>/error.xhtml</location>
</error-page>
</web-app>
And faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<managed-bean>
<managed-bean-name>managedBeann</managed-bean-name>
<managed-bean-class>hello.ManagedBeann</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Because nonworking annotations is used.
By the way PrimeFaces is working.
My purpose is force JSF-annotation to work, because in real project without them it is impossible.

Disclaimer
I am answering this based on what I think you were trying to acheive even though my answer does not match the question title.
You said "My purpose is force JSF-annotation to work, because in real project without them it is impossible." I'm guessing you mean "impossible" because putting managed beans in the faces-config.xml is cumbersome. So to this end I am going to not use the faces-config.xml to manage beans.
I'm going to show you an alternative that uses Spring annotations which is very non-cumbersome and I feel accomplishes your original goal.
Answer
Example --
https://github.com/Zergleb/Spring-Boot-JSF-Example
I looked over your question the other day and decided to try and make this work and I put my results on github (Link above). This example should allow you to write a JSF application using Spring annotations instead of JSF annotations for example you'll say
#Component
#Scope("view")
//The example above contains an implementation of the View Scope in Spring.
instead of
#ManagedBean
#ViewScope
and you'll then be able to use Spring for all of your dependency injection.
I used gradle instead of maven so this means your dependencies are in the build.gradle instead of the pom.xml I had to add these in order to make everything work. Those should be easy enough to translate to a pom.xml I imagine.
compile group: 'javax.el', name: 'el-api', version: '1.0'
compile group: 'com.sun.el', name: 'el-ri', version: '1.0'
compile group: "javax.servlet.jsp" name: "jsp-api" version: "2.1"
My web.xml only has one servlet now and I removed the servlet-mapping and all of the other attributes of the web.xml
(I'm still working on how to remove this web.xml altogether check the example for any updates on whether I figured it out or not)
<web-app ... same as before>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
</web-app>
faces-config.xml now has no managed beans
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I do not have this right now but we might want to consider having an empty in the web.xml I haven't researched this a ton but one of the spring-project examples on github contains this code
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml
<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
I hope that answers your question. If I left something out try and reference the example code.
Example
https://github.com/Zergleb/Spring-Boot-JSF-Example
Runs a spring boot application that should both run Spring MVC and JSF in one application sharing a common context.(I included this in the answer because you referenced this link in your question Spring Boot and JSF/Primefaces/Richfaces which says that mixing Spring MVC and JSF is impossible but I have working in my example code.

Related

Tomcat 6 vs 7 broke default and jsp servlet requests (404)

We're migrating around 20 web contexts from Tomcat 6.0.48 to Tomcat 7.0.76 but we're facing an issue with the requests handled by the servlets configured in the parent web.xml (tomcat7\conf\web.xml). The problem is that requests for /some_file.html, /some_file.jsp, /images/some-image.jpg, /index.xhtml return a 404 NOT FOUND, while other custom servlets declared in the web.xml of the context work fine. All 20 contexts are working fine in Tomcat 6 for years, but only 10 are failing on Tomcat 7. We have compared the ones that work with the ones that don't, but they are very different (unsurprisingly) and we haven't found the problem.
The tests
Projects are deployed using Eclipse under Windows, some test were executed deploying the WAR file directly on /webapps with the same results. All projects are Java 1.8 and they are mavenized. The tests consist in deploying one working context and one that does not work and navigate to: /some_file.html, /some_file.jsp, /images/some-image.jpg and see if the response is 200 or 404.
We've tried to match the version of javax.servlet.servlet-api (2.5 to 3.0.1) and javax.servlet.jsp.jsp-api (2.0 to 2.2) with the ones provided by Tomcat 7 in the dependencies (Parent POM). No change.
<dependency>
<groupId>javax.servlet</groupId>
<!-- <artifactId>servlet-api</artifactId>
<version>2.5</version> -->
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<!-- <version>2.0</version> -->
<version>2.2</version>
<scope>provided</scope>
</dependency>
We've copied the default and jsp servlet mapping configuration from the web.xml file of Tomcat on the web.xml of the application. No change.
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
We've changed the url-pattern / to /* in the tomcat's web.xml file for the default servlet and things started to work with static resources but not for *.jsp and *.xhtml files (Faces). But as read in the servlet specification (JSR-315), it should work with /. Either way, this change broke *.jsp requests for all contexts (¿?).
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
<servlet-name>default</servlet-name>
<url-pattern>/*</url-pattern>
We've deleted all servlet-mapping and filter-mapping from the web.xml file of the context to see if there was any kind of conflict. No change.
We suspect that...
either the tomcat's web.xml is not read, it's overwritten, or the url-pattern for / and *.jsp is broken by some contexts's descriptor.
Any clues?
Thanks in advance.

spring boot application deployment on weblogic throws 404 error

I have done below configurations and tried almost all solutions found but nothing helped. When i am deploying spring boot app in war package. no error got logged in weblogic log but the application throwing 404 error.
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:root-context.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.liveBeansView.mbeanDomain</param-name>
<param-value>dev</param-value>
</context-param>
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wls="http://www.bea.com/ns/weblogic/90"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
<wls:weblogic-version>12.1.2.0.0</wls:weblogic-version>
<wls:context-root>/services/userModule/</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>com.fasterxml</wls:package-name>
<wls:package-name>org.slf4j</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
application.properties
spring.profiles.default=default
spring.profiles.active=default
spring.liveBeansView.mbeanDomain=default
cms.config.monitor.dir=/server/location/application/artifacts
application.messages.file.name=application-messages
application.config.file.name=application-config
root-context.xml
it contains application specific configurations.
ApplicationBegin.java
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
public class ApplicationBegin extends SpringBootServletInitializer implements WebApplicationInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApplicationBegin.class);
}
public static void main(String[] args){
SpringApplication.run(ApplicationBegin.class, args);
}
Cannot exclude tomcat server from pom.xml as it is failing the compilation. is there a way to set tomcat as provided while using spring boot starter web?
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<!-- THIS WILL BE EXCLUDE ONLY FOR WEBLOGIC DEPLOYMENT -->
<!-- <exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion> -->
</exclusions>
</dependency>
The problem is the application runs fine with embedded tomcat but it is not working even not throwing any error when deploying on weblogic. Where should i look?
Can you try class loading as the parent last? So Spring boot will use it's own container libraries.
After trying the solutions i found from different people , it couldn't solve my issue and somehow i have solved it now. all the similar issues i have seen on this topic and the soultions, i finally understood no answer was actually a solution because in most of the cases the issue happens because of wrong configuration that weblogic dosen't understand. the worst part being it doesn't even throw error. In my case other than application.properties file and a root-context.xml, i explicitly specified web.xml file in /WEB-INF location and defined context-config location there. As soon as i removed the web.xml and refactored/filtered the project dependencies from top to bottom , it resolved the issue.
and also then i realized many handy solutions on the web for this issue wouldn't even be required if your configuration is correct. for an example, configuring a jpavendor won't require if you use spring boot jpa starter correctly.
so.. if you ever face this kind of deployment issue on weblogic , you may follow below steps -
Only deploy a bareminimum part of application and make it workable
on weblogic
then add your critical dependencies / configurations and deploy them one by one on weblogic and check if it is working
you should always run your boot application to other local server first for resolving major configuration issues ..tomcat is
good.
I had the same problem, but I finally managed to solve it.
The problem was the version of web.xml descriptor. If you put the web.xml file in your project with <web-app version="2.5">, even though your Weblogic supports servlet 3.0, the spring controllers would throw 404.
That also explains the behavior of your app - what's why it started working when you removed the web.xml file.

Restful site broke after upgrading resteasy-jaxrs to the lastest version

I'm going through this tutorial example on RestEasy:
http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/
I downloaded their code and made modification so that I can deploy it to tomcat 7 and java 1.7.
If I leave the pom.xml as specified by the site,
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
then everything appears to be fine and can be accessed through:
http://localhost:8080/RESTfulExample/rest/message/hello
However, if I were to increase the version level to 3.0.8.Final or "RELEASE",
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.8.Final</version>
</dependency>
then I can't access it via the above URL. Instead, I get this message in my localhost_access_log.txt
127.0.0.1 - - [19/Aug/2014:16:02:55 -0700] "GET /RESTfulExample/rest/message/hello HTTP/1.1" 404 -
Question: Does anyone know how I can get the pom.xml to work if I really want to use RESTeasy 3.0.8.Final? I'm new to Rest.
Thanks in advance.
As the documentation describes you can initialize RESTeasy in a standalone Servlet 3.0 compliant container by adding this dependency:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.8.Final</version>
</dependency>
You should also update the web.xml with the correct Servlet version. Most of the old configuration stuff can be removed so you end up with:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Restful Web Application</display-name>
</web-app>
Last thing to do is tell RESTeasy on which path you want to map your application by adding javax.ws.rs.ApplicationPath to the MessageApplication class:
#ApplicationPath("/rest")
public class MessageApplication extends Application {
...
}

How to add Swagger to a Jersey web app that only uses a ResourceConfig?

We have started working on a web-app with a REST api and I found Swagger and I really want it... However, I am struggling to figure out how to add it in our app. The app is declared like this (hypothetical):
package hello.world;
import org.glassfish.jersey.server.ResourceConfig;
public class CloudApplication extends ResourceConfig {
public CloudApplication() {
// Add all resources from hello.world
packages("hello.world", "com.fasterxml.jackson.jaxrs");
}
}
And the project has a web.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>hello.world</display-name>
<servlet>
<servlet-name>hello.world.CloudApplication</servlet-name>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>hello.world.CloudApplication</servlet-name>
<url-pattern>/hw/*</url-pattern>
</servlet-mapping>
</web-app>
My pom.xml file contains
...
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<scope>compile</scope>
<version>1.3.0</version>
</dependency>
...
Looking at https://github.com/wordnik/swagger-core/tree/master/samples, I find all the different setup, but I can't figure out which to use. https://github.com/wordnik/swagger-core/tree/master/samples/java-jaxrs seems like the right one, but can I keep the current project structure with just a resource config? /samples/java-jaxrs seems to require a lot of web.xml stuff... Can I do that in code instead? Any sample out there?
I do not have so much experience with web apps, but I would love to get this Swagger thing to work.
// Jonas
Well, its a bit of pain. I ended up with the following, maybe it's just another setup that will confuse you, but maybe it works for you too:
I use a small bootstrap class to configure swagger like this:
public class SwaggerBootstrap extends HttpServlet {
#Override public void init(ServletConfig servletConfig) {
ConfigFactory.config().setBasePath(basepath);
ConfigFactory.config().setApiPath(apipath);
ConfigFactory.config().setApiVersion(apiversion);
ConfigFactory.config().setSwaggerVersion(com.wordnik.swagger.core.SwaggerSpec.version());
}
}
My web.xml contains:
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.wordnik.swagger.jersey.listing</param-value>
</init-param>
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>SwaggerBootstrap</servlet-name>
<servlet-class>my.path.SwaggerBootstrap</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
And my pom extract is here:
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.10</artifactId>
<version>1.3.4</version>
</dependency>
Good luck;)

JAX WS webservice does not take spring bean from applicationcontext, hence throws null pointer exception

Hi I have got the webservice up and running , i have used jax ws. I have used Spring to be able to use beans with Autowired and stuff that spring gives like property value injection in applicationContext.xml.
I have the below spring applicationcontext.xml entry:
<context:component-scan base-package="com.mybeans.service" />
<bean id="myProperty" class="com.mybeans.service.MyBeanProperty"
p:Size="BIG">
</bean>
In web service end point class , i have done:
#Autowired private MyBeanProperty myProperty;
And I have a method :
public String getSize() {
return myProperty.getSize();
}
Unfortunately when i invoke the method it does not get any value and throws nullpointerexception.
PS: I used soapUI to run the wsdl of the webservice and invoked the method.
Is the webservice runs before the beans get created by Spring??
To duffmo
Yes i used component scan in applicationContext. And i do have the context loader listener as below in web.xml. Please help me..
Here is my complete code explainaton with code
I am using JAX-WS and Spring and try to setup a few WebServices which need run on Tomcat 7.
I am using Maven as build tool therefore I just list my two dependencies here:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
my service classes are located in com.test.services and are named TestService & HelloWorldService and look as follows:
package com.test.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService( name = "Test", serviceName = "TestService" )
public class TestService {
#WebMethod
public String getTest() {
return "Test";
}
}
this is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>toolbox</display-name>
<description>testing webservices</description>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>jaxws-servlet</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>jaxws-servlet</servlet-name>
<url-pattern>/testservice</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/helloworldservice</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
and this is my sun-jaxws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name="jaxws-servlet"
implementation="com.test.services.TestService"
url-pattern="/testservice"/>
<endpoint
name="jaxws-servlet"
implementation="com.test.services.HelloWorldService"
url-pattern="/helloworldservice" />
</endpoints>
This works great and I can access the services by pointing my browser to [url]http://localhost:8080/toolbox/testservice[/url] respectively [url]http://localhost:8080/toolbox/helloworldservice[/url].
However Spring support is obviously not activated.
I tried the following which just leaver the HelloWorldService available:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>toolbox</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
and applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.test.services" />
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/" />
</bean>
</beans>
furthermore I annotated both Service classes with #Service annotation. As I mentioned before, this only publishes the alphabetically first webservice, hence HelloWorldService.
Also it changes the URL, as the service is now available as [url]http://localhost:8080/[/url] rather than [url]http://localhost:8080/toolbox/helloworldservice[/url].
The logging of Tomcat shows, that the Spring Context loads both Classes as Spring beans.
Do you have any ideas or suggestions on how to enable Spring support while keeping both services available??
It is answered here. Ultimately nothing worked other than adding below code to service impl.
#PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
No Need to use ApplicationContext as well, if you do the following things
Annotate your service class with #Service
Service class should extends "SpringBeanAutowiringSupport".
Please have a look at the following snippet.
#org.springframework.stereotype.Service
#javax.jws.WebService (endpointInterface="a.b.c.MyPort",
targetNamespace="http://a.b.co.in/Retail/MyService/V1",
serviceName="MyService",
portName="MyServicePort",
wsdlLocation="wsdl/MyService.wsdl")
public class MyServiceBindingImpl extends org.springframework.web.context.support.SpringBeanAutowiringSupport{
I got the same issue, to resolve it I started the jaxws listener after the spring listener:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
Hope it helps
Your TestService (which is annotated with #WebService) should extend "SpringBeanAutowiringSupport" class to kick start spring binding.
Please have a look at the duffmo mentioned points as well.
I think it's more likely that your Spring context has not be loaded and made available to the web service. How have you done that?
You should have a ContextLoaderListener configured in the web.xml for the WAR in which the web service is deployed. Did you tell it where to load the Spring context? Are you using component scan?
http://renidev.wordpress.com/2009/02/02/how-to-use-springs-context-component-scan-and-annotation/
You misses the configuration for webservice inject. So put more inside the web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
Please don't forget the order. Because you need to init the bean for the autowired field first
Thanks

Resources