當前位置:文思屋>社會工作>IT認證>

整合spring與Web容器教程

文思屋 人氣:1.62W

spring框架的主要優勢之一就是其分層架構,分層架構允許使用者選擇使用哪一個元件,同時為 J2EE 應用程式開發提供整合的`框架。下面本站小編為大家準備了關於整合spring與Web容器教程,歡迎閱讀。

1.建立HelloWorld 介面類

package com.googlecode.garbagecan.cxfstudy.helloworld;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

@WebService

public interface HelloWorld {

@WebMethod

@WebResult String sayHi(@WebParam String text);

}

2.建立HelloWorld實現類

package com.googlecode.garbagecan.cxfstudy.helloworld;

public class HelloWorldImpl implements HelloWorld {

public String sayHi(String name) {

String msg = "Hello " + name + "!";

return msg;

}

}

3.修改web.xml檔案

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

cxfstudy

cxf

org.apache.cxf.transport.servlet.CXFServlet

1

cxf

/ws/*

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath*:**/spring.xml

4.建立spring配置檔案並放在classpath路徑

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">

5.建立測試

package com.googlecode.garbagecan.cxfstudy.helloworld;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringClient {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

HelloWorld helloworld = (HelloWorld)context.getBean("helloworldClient");

System.out.println(helloworld.sayHi("kongxx"));

}

}

6.測試

6.1 首先啟動tomcat或者使用maven的jetty,並訪問http://localhost:9000/ws/HelloWorld?wsdl來驗證web service已經啟動並且生效;

6.2 然後執行測試類來驗證web service。