This does not introduce the principle, just to record their own SPRING+CXF development process and the problems encountered
Scenario: A third-party company needs to call our business system to pass data in the form of XML messages, and then we parse the message to store it in our database to generate business documents;
WebService frame by a variety of, here choose CXF, and spring integration is better;
Look directly at the code
1 Project with MAVEN, first add dependencies (this dependency ah, tutorial reference a few of them have, this see need, I was used four)
<dependency> <groupId>org.apache.cxf</groupId> <ARTIFACTID>CXF </artifactId> <version>2.7.11</version> <type>pom</type> </dependency> <dependency> <groupId>org.apache.cxf</groupId> &L T;artifactid>cxf-rt-frontend-jaxws</artifactid> <version>2.7.11</version> </ dependency> <dependency> <groupId>org.apache.cxf</groupId> <art Ifactid>cxf-rt-transports-http</artifactid> <version>2.7.11</version> </depe ndency> <dependency> <groupId>org.apache.cxf</groupId> <art Ifactid>cxf-rt-transports-http-jetty</artifactid> <version>2.7.11</version> </dependency>
2 config Web. Xml, added on original basis (here I just add cxf configuration, Spring in the basic configuration before the project must have, like what context-param what I think you must have already had)
<!--cxf-- <servlet> <servlet-name>CXFServlet</servlet-name> <servlet- class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> </servlet > <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern >/webservice/*</url-pattern> </servlet-mapping>
3 Configure Applicationcontext.xml (or add the following on the original basis)
3.1 First in the XML to add a namespace, which I added on the original base (the red part I added myself)
3.2 Added on original basis
<!--CXF Configuration- <import resource= "Classpath:meta-inf/cxf/cxf.xml"/> <import resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/> <import resource= "Classpath: Meta-inf/cxf/cxf-servlet.xml "/> class=" Com.ufgov.lp.xml.sax.handler.ReimBillHandler "/> class= "Com.ufgov.lp.bill.webservice.impl.ReciveBillServiceImpl" > <property name= " Reimbillhandler "ref=" Reimbillhandler "></property> </bean> <jaxws:endpoint id=" Recivebillservice " implementor=" #reciveBillServiceImpl "address="/recivebillservice "/> <!--CXF configuration ends--
Next start writing code
4 Defining interfaces
import Javax.jws.WebMethod; import Javax.jws.WebParam; import Javax.jws.WebService; /** * * <p> Receive Message interface </p> * @author SHANGCG * @since November 1, 2017 */ @WebService public interface Recivebillservice {@WebMethod public stri Ng request (@WebParam (name = "Xmlstr"
5 Creating an implementation class
ImportJavax.annotation.Resource;ImportJavax.jws.WebService;ImportCom.ufgov.lp.bill.webservice.ReciveBillService;ImportCom.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect;ImportCom.ufgov.lp.xml.sax.handler.ReimBillHandler; @WebService Public classRecivebillserviceimplImplementsRecivebillservice {@Resource Reimbillhandler reimbillhandler; @Override Publicstring Request (String xmlstr) { Try { /**JavaBean objects that are consistent with the database structure*/Lpbizbilldatacollect Lpbizbilldatacollect=NewLpbizbilldatacollect (); //The incoming message is loaded directly into the object (in fact, the business system generally has to parse the XML, this does not say that parsing)lpbizbilldatacollect.setfield01 (XMLSTR); //inserting a database (Reimbillhandler injection in order to invoke a method)Reimbillhandler.excutebillinsert (Lpbizbilldatacollect); } Catch(Exception e) {e.printstacktrace (); } return"Here is the message returned."; } PublicReimbillhandler Getreimbillhandler () {returnReimbillhandler; } //Note the Set method here, or the bean will not be injected after you configure it later Public voidSetreimbillhandler (Reimbillhandler reimbillhandler) { This. Reimbillhandler =Reimbillhandler; }}
6 See Reimbillhandler (This class is a business system already exists in the @service labeled class, successfully injected into the class, if the class is injected into other classes we can do without the tube)
Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.ufgov.lp.bill.dao.LpBizBillDao;ImportCom.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect;/*** * <p> inject Lpbizbilldao Insert Data </p> *@authorSHANGCG *@sinceNovember 3, 2017*/@Service Public classreimbillhandler{@Autowired Lpbizbilldao Lpbizbilldao; /**inserting database table data*/ Public intExcutebillinsert (Lpbizbilldatacollect lpbizbilldatacollect) {returnLpbizbilldao.insert (Lpbizbilldatacollect);//inserting database Data } }
Tell me about the errors I encountered in these processes:
1 Spring Bean injection does not come in to solve: the class with @webservice annotation, reference spring bean thing need to do two things, the first is injected with @resource annotation, not @autowire; The second must have a set method; The third is that APPLICATIONCONTEXT.XM must configure the bean and the reference
SPRING+CXF Development WebService (mainly to record the solution that spring bean injection does not come in)