Problem. WebService and Spring integration process
The previous one mentions the WebService overall development process, which is mainly to say that the integration process between WebService and spring
First, ask questions
Question: In previous projects, we all need to publish the project to the server, and WebService is also a service, if according to the idea, we need to publish two services to make the entire project run smoothly, and the port number is not the same, so that service two , Port two , which is not particularly complicated. If there is a way to make it release the service, the WebService service can also be opened and the port number is one. Obviously we have a way, we integrate it with spring and give it to spring for management.
Second, the integration process:
1 The bean of Spring management business system,Beans.xml (Spring Foundation)
2) WebService CXF cxf-servlet.xml(CXF service file)
3 to Configure Spring and CXF in Web.xml (this is definitely to be added to the Web.xml)
1. Enter jar bag: This jar bag can use the website cxf download lib inside have spring jar package, do not download. Click on the download link
Create a new WebProject project, take the jar package into the Lib
2. Create a new Cxfservice service class to open the service.
Package com.itcast.ws;
Import Javax.jws.WebService;
/*import org.apache.cxf.jaxws.jaxwsserverfactorybean;*/
@WebService (servicename= "cxfservices")
public Class Cxfservice {public
string Hello (string name) {
System.out.println ("has entered server:" +name);
Return "value returned from server:" +name
}
/* Public static void Main (string[] args) {
Jaxwsserverfactorybean bean = new Jaxwsserverfactorybean ();
Bean.setaddress ("Http://localhost:9090/hello");
Bean.setserviceclass (cxfserver.class);
Bean.create ();
SYSTEM.OUT.PRINTLN ("Service already open!");
}
///traditional direct running of this class will be able to post service services. But now 㤇 direct and spring whole don't need to use this way anymore
}
3.1: Let's take a look at the configuration when Cxf-servlet is under Web-inf :
<?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:jaxws= "Http://cxf.apache.org/jaxws"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<import Resource = "Classpath:meta-inf/cxf/cxf.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
<!--somewhat similar to the JDK's form parameter address is the access to addresses Implementor is the implementing person ( The first test can use the service class Com.itcast.ws.CXFService, but it is 㤇 to use the Bean's #id later when consolidating with the spring configuration file-->
<jaxws:endpoint Implementor= "Com.itcast.ws.CXFService" address= "/hello"/>
</beans>
3.2. Look again when the
cxf-servlet.xml and beans.xml under SRCThe time configuration:
<?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:jaxws= "Http://cxf.apache.org/jaxws"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<import Resource = "Classpath:meta-inf/cxf/cxf.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
<!--somewhat similar to the JDK's form parameter address is the access to addresses Implementor is the implementing person ( The first test can use the service class Com.itcast.ws.CXFService, but it is 㤇 to use the Bean's #id later when consolidating with the spring configuration file-->
<jaxws:endpoint Implementor= "#cxfServices" address= "/hello"/>
<import resource= "Beans.xml"/>
</beans>
4.
Beans.xmlFile (Spring)
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/
Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/context htt P://www.springframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!--this is the Spring Foundation, put the service class in, give Spring management--> <bean id= "cxfservices" class= "Com.itcast.ws.CXFService" ></bean> </beans >
5.web.xml file configuration information:
<?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></display-name> <!--integration Spring Serv Let--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>c lasspath:beans.xml</param-value> </context-param> <listener> <listener-class> Org.springframework.web.context.contextloader</listener-class> </listener> <!--cxf and servlet consolidation only manages roads
Path for CXF (/cxf/*) under the content of the source will know that it will find Cxf-servlet.xml file--> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <!-- If Cxf-servlet.xml is under Web-inf, you don't need to configure it, but if you need to configure the name under SRC, why is Config-locatioN, look at the Cxfservlet source code will know. --> <init-param> <param-name>config-location</param-name> <PARAM-VALUE>CLASSPATH:CXF -servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <!--access to WSDL files You need access to this name, and you can find Web services--> <servlet-name>cxf</servlet-name> <url-pattern>/cxf/*</ url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-fi
Le> </welcome-file-list> </web-app>
Here's a question to ask. It through
CXFThe name was found
Org.apache.cxf.transport.servlet.CXFServletAfter how to find
Cxf-servlet.xmlFile. We'll catch the source and we'll know.
Tomcate run, look at the results: HTTP://LOCALHOST:8080/CXFSPRINGPROJECT/CXF
finally successful integration.
The whole WebService learning process is over here. The whole learning process of information and case download: Click on the Open link