文章目录
需要的maven依赖
<!-- spring配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!--cxf的配置-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.5</version>
</dependency>
服务接口和实现类都在com.service包下
服务接口SendService
@WebService
public interface SendService {
String sendOA(String param);
String sendOrg(String org);
}
接口实现类SendServiceImpl
public class SendServiceImpl implements SendService {
@Override
public String sendOA(String param) {
System.out.println("===" + param);
return "hellow:" + param;
}
@Override
public String sendOrg(String org) {
return "false";
}
}
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
ContextLoaderListener的作用
就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法,使用ServletContextListener接口,开发者能够在为客户端请求提供服务之前向ServletContext中添加任意的对象。这个对象在ServletContext启动的时候被初始化,然后在ServletContext整个运行期间都是可见的。
每一个Web应用都有一个ServletContext与之相关联。ServletContext对象在应用启动时被创建,在应用关闭的时候被销毁。ServletContext在全局范围内有效,类似于应用中的一个全局变量。
在ServletContextListener中的核心逻辑便是初始化WebApplicationContext实例并存放至ServletContext中。
contextConfigLocation配置参数的作用
告诉ContextLoader需要加载额外配置文件路径
在执行ContextLoaderListener里面的contextInitialized方法时会调用ContextLoader类的initWebApplicationContext方法,此方法内会调用configureAndRefreshWebApplicationContext方法,此方法内获取去contextConfigLocation参数的配置值,加载配置
applicationContext.xml配置文件的配置
<?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:p="http://www.springframework.org/schema/p"
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-extension-soap.xml"/>-->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:server address="/server">
<jaxws:serviceBean>
<bean class="com.service.SendServiceImpl"></bean>
<!-- <bean class="com.service.impl.PersonServiceImpl"></bean>-->
</jaxws:serviceBean>
</jaxws:server>
<!-- <jaxws:client id="sendServiceClient" serviceClass="com.service.SendService"
address="http://10.137.138.11:9080/Wb/webservice/sendServie?wsdl" />-->
</beans>
Tomcat启动配置
根据Tomcat的启动路径和applicationContext.xml配置中服务路径访问http://localhost:8080/webservice/server?wsdl,即可获取wsdl文件