java - how to bootstrap Spring in a JbossWS-CXF web service deployed in jboss AS 7.2 -


tldr @ bottom:

as per jbossws-cxf user guide, web service, web.xml should contain following

    <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">         <servlet>             <servlet-name>mywebservice</servlet-name>             <servlet-class>com.sgb.mywebservice</servlet-class>         </servlet>         <servlet-mapping>             <servlet-name>mywebservice</servlet-name>             <url-pattern>/*</url-pattern>         </servlet-mapping>     </web-app> 

jboss expects descriptor file named jboss-cxf.xml in web-inf directory (instead of cxf.xml) should contain jaxws:endpoint tag so:

            <beans xmlns='http://www.springframework.org/schema/beans'         xmlns:xsi='http://www.w3.org/2001/xmlschema-instance'          xmlns:beans='http://www.springframework.org/schema/beans'         xmlns:jaxws='http://cxf.apache.org/jaxws'         xsi:schemalocation='http://www.springframework.org/schema/beans                  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd             http://cxf.apache.org/jaxws >          <bean id="mywebservice" class="com.sgb.mywebservice" />          <jaxws:endpoint id="pojoendpoint" implementor="#mywebservice" wsdllocation="web-inf/wsdl/xyz.wsdl" address="/warfilename">             <jaxws:invoker>                 <bean class="org.jboss.wsf.stack.cxf.invokerjse" />             </jaxws:invoker>         </jaxws:endpoint>     </beans> 

i create service implementation class thusly:

    package com.sgb;      @javax.jws.webservice(... ... ... )     public class mywebservice implements imywebservice     {         public createresponse create(createrequest request)         {             ... ... ... <-- instance of createservice created             return createservice.serve(request)         }     } 

so far good. works fine.

however, per spring's reference documentation, convenient way instantiate application context web applications adding contextloaderlistener in web.xml so.

    <context-param>         <param-name>contextconfiglocation</param-name>         <param-value>/web-inf/applicationcontext.xml</param-value>     </context-param>     <listener>         <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener> 

so, add above in web.xml , annotate mywebservice class @service , make sure package set component-scaning. , should become spring managed bean too.

problem is, doesn't. jbossws-cxf seems instantiating mywebservice due dependencies not injecte resulting in nullpointer.

i able applicationcontext programmatically using classpathxmlapplicationcontext("/web-inf/applicationcontext.xml") , inject/create dependencies using appcontext.getbean()

but hoping inject/autowire dependencies directly using annotations instead.

tldr:

what have this. (this bean created jboss , not spring):

    @javax.jws.webservice(... ... ... )     public class mywebservice implements imywebservice     {         private applicationcontext appcontext;         public mywebservice(){             appcontext = new classpathxmlapplicationcontext("/meta-inf/spring/applicationcontext-ws.xml");         }          public createresponse create(createrequest request)         {                             *** use getbean() here dependency. ***             ixyzservice createservice = appcontext.getbean("createservice",ixyzservice.class);             return createservice.serve(request)         }     } 

what want this:

    @javax.jws.webservice(... ... ... )     @service    <-- <-- <-- ** spring managed bean**     public class mywebservice implements imywebservice     {         @resource <-- <-- <-- **dependency injected spring**         ixyzservice createservice;          public createresponse create(createrequest request)         {             return createservice.serve(request)         }     } 

what best way accomplish ???

i found out few days indeed possible, editing previous answer. magic glue this:

@postconstruct public void postconstruct(){     springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this); } 

so summarize, following pieces needed:

1) load spring context via web.xml

<context-param>     <param-name>contextconfiglocation</param-name>     <param-value>classpath*:/meta-inf/spring/appcontext.xml</param-value> </context-param>  <listener>     <listener-class>         org.springframework.web.context.contextloaderlistener     </listener-class> </listener> 

2) annotate method @postconstruct in class implements interface generated cxf so:

@javax.jws.webservice(... ... ... ) public class mywebservice implements imywebservice {     @resource <-- <-- <-- **dependency injected spring**     ixyzservice createservice;       @postconstruct     public void postconstruct(){         springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this);     }      public createresponse create(createrequest request)     {         return createservice.serve(request)     } } 

the above info courtesy of link:how initialize spring framework inside cxf jax-ws service hope helps...

=== previous answer ===

well. turns out, cannot done - version have working correct way - far can tell.

explanation:

apache cxf can used or without spring.

jbosscxf (in 7.x or eap 6.x) uses cxf default web service stack without spring. when jboss invokes class implements web service interface (mywebservice above in example annotated @webservice(), spring container not yet initiated.... configuration in web.xml or jboss-cxf.xml not allow it.

so, spring container needs manually started inside sei impl class due service class itself cannot spring managed bean (obviously).

hence, need instantiate service beans inside method using getbean() method.

once service beans instantiated, dependencies automatically managed spring container spring managed bean.

hope helps someone.

sgb


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -