依赖spring.jar、commons-dbcp-1.4.jar、commons-pool-1.5.6.jar、
cglib-nodep-2.2.jar
jdbc.properties代码:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.10:1521:oraid
jdbc.username=shihuan
jdbc.password=zznode
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>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->
<!-- <util:properties id="jdbcProperties" location="classpath:jdbc.properties"/> -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"></bean> -->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"></bean> -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></bean> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:oraid" />
<property name="username" value="shihuan" />
<property name="password" value="zznode" />
-->
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="50" />
<property name="minIdle" value="5" />
</bean>
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="springAwareService" class="axis2.service.server.SpringAwareService">
<property name="springJdbcBean" ref="springJdbcBean"/>
</bean>
<bean id="springJdbcBean" class="axis2.service.springjdbc.SpringJdbcBeanImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
SpringJdbcBean.java代码:
package axis2.service.springjdbc;
public interface SpringJdbcBean {
public String[] getArray(String paramName);
public String[] getMDArray(String paramName);
public String getXmlData(String paramName);
}
SpringJdbcBeanImpl.java代码:
package axis2.service.springjdbc;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class SpringJdbcBeanImpl extends JdbcDaoSupport implements SpringJdbcBean {
public String[] getArray(String paramName) {
String sql = "SELECT T.MODULE_NAME, T.PARAM_NAME, T.SHOW_NAME, T.REL_SYMBOLS, T.GREEN_VALUE, T.YELLOW_VALUE, T.RED_VALUE, T.UNIT, T.SCPOE FROM TB_THRESHOLD_SETTING T WHERE T.PARAM_NAME = ?";
Map map = getJdbcTemplate().queryForMap(sql, new Object[]{paramName});
String[] strArray = null;
if(map.size() > 0){
strArray = new String[9];
strArray[0] = (String) map.get("MODULE_NAME");
strArray[1] = (String) map.get("PARAM_NAME");
strArray[2] = (String) map.get("SHOW_NAME");
strArray[3] = (String) map.get("REL_SYMBOLS");
strArray[4] = String.valueOf((BigDecimal) map.get("GREEN_VALUE"));
strArray[5] = String.valueOf((BigDecimal) map.get("YELLOW_VALUE"));
strArray[6] = String.valueOf((BigDecimal) map.get("RED_VALUE"));
strArray[7] = (String) map.get("UNIT");
strArray[8] = String.valueOf((BigDecimal) map.get("SCPOE"));
}else{
strArray = new String[]{"nodata"};
}
return strArray;
}
public String[] getMDArray(String paramName) {
String sql = "SELECT T.MODULE_NAME, T.PARAM_NAME, T.SHOW_NAME, T.REL_SYMBOLS, T.GREEN_VALUE, T.YELLOW_VALUE, T.RED_VALUE, T.UNIT, T.SCPOE FROM TB_THRESHOLD_SETTING T WHERE T.PARAM_NAME LIKE '%" + paramName + "%'";
List list = getJdbcTemplate().queryForList(sql);
int listLen = list.size();
String[] strArray = null;
if(listLen > 0){
strArray = new String[listLen];
for(int i=0; i<listLen; i++){
Map map = (Map)list.get(i);
StringBuffer sb = new StringBuffer();
sb.append((String)map.get("MODULE_NAME"))
.append(",")
.append((String)map.get("PARAM_NAME"))
.append(",")
.append((String)map.get("SHOW_NAME"))
.append(",")
.append((String)map.get("REL_SYMBOLS"))
.append(",")
.append(String.valueOf((BigDecimal) map.get("GREEN_VALUE")))
.append(",")
.append(String.valueOf((BigDecimal) map.get("YELLOW_VALUE")))
.append(",")
.append(String.valueOf((BigDecimal) map.get("RED_VALUE")))
.append(",")
.append((String) map.get("UNIT"))
.append(",")
.append(String.valueOf((BigDecimal) map.get("SCPOE")));
strArray[i] = sb.toString();
}
}else{
strArray = new String[]{"nodata"};
}
return strArray;
}
public String getXmlData(String paramName) {
String sql = "SELECT T.MODULE_NAME, T.PARAM_NAME, T.SHOW_NAME, T.REL_SYMBOLS, T.GREEN_VALUE, T.YELLOW_VALUE, T.RED_VALUE, T.UNIT, T.SCPOE FROM TB_THRESHOLD_SETTING T WHERE T.PARAM_NAME LIKE '%" + paramName + "%'";
List list = getJdbcTemplate().queryForList(sql);
String xmlStr = "";
Document document = DocumentHelper.createDocument();
if(list.size() > 0){
if(list.size() == 1){
Map map = (Map)list.get(0);
Element root = document.addElement("datas");
Element modulename = root.addElement("modulename").addText((String) map.get("MODULE_NAME"));
Element paramName1 = root.addElement("paramName").addText((String)map.get("PARAM_NAME"));
Element showName = root.addElement("showName").addText((String)map.get("SHOW_NAME"));
// Element relSymbols = root.addElement("relSymbols").addText((String)map.get("REL_SYMBOLS"));
Element relSymbols = root.addElement("relSymbols").addCDATA((String)map.get("REL_SYMBOLS"));
Element greenValue = root.addElement("greenValue").addText(String.valueOf((BigDecimal) map.get("GREEN_VALUE")));
Element yellowValue = root.addElement("yellowValue").addText(String.valueOf((BigDecimal) map.get("YELLOW_VALUE")));
Element redValue = root.addElement("redValue").addText(String.valueOf((BigDecimal) map.get("RED_VALUE")));
Element unit = root.addElement("unit").addText((String) map.get("UNIT"));
Element scpoe = root.addElement("scpoe").addText(String.valueOf((BigDecimal) map.get("SCPOE")));
}else{
Element root = document.addElement("datas");
for(int i=0; i<list.size(); i++){
Map map = (Map)list.get(i);
Element leaf = root.addElement("data");
Element modulename = leaf.addElement("modulename").addText((String) map.get("MODULE_NAME"));
Element paramName1 = leaf.addElement("paramName").addText((String)map.get("PARAM_NAME"));
Element showName = leaf.addElement("showName").addText((String)map.get("SHOW_NAME"));
// Element relSymbols = leaf.addElement("relSymbols").addText((String)map.get("REL_SYMBOLS"));
Element relSymbols = leaf.addElement("relSymbols").addCDATA((String)map.get("REL_SYMBOLS"));
Element greenValue = leaf.addElement("greenValue").addText(String.valueOf((BigDecimal) map.get("GREEN_VALUE")));
Element yellowValue = leaf.addElement("yellowValue").addText(String.valueOf((BigDecimal) map.get("YELLOW_VALUE")));
Element redValue = leaf.addElement("redValue").addText(String.valueOf((BigDecimal) map.get("RED_VALUE")));
Element unit = leaf.addElement("unit").addText((String) map.get("UNIT"));
Element scpoe = leaf.addElement("scpoe").addText(String.valueOf((BigDecimal) map.get("SCPOE")));
}
}
}else{
Element root = document.addElement("nodata");
}
xmlStr = document.asXML();
return xmlStr;
}
}
SpringAwareService.java代码:
package axis2.service.server;
import axis2.service.springjdbc.SpringJdbcBean;
public class SpringAwareService {
private SpringJdbcBean springJdbcBean = null;
public void setSpringJdbcBean(SpringJdbcBean springJdbcBean) {
this.springJdbcBean = springJdbcBean;
}
public String[] getArray(String paramName) {
return this.springJdbcBean.getArray(paramName);
}
public String[] getMDArray(String paramName) {
return this.springJdbcBean.getMDArray(paramName);
}
public String getXmlData(String paramName) {
return this.springJdbcBean.getXmlData(paramName);
}
}
services.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="ComplexTypeService">
<description>Multy Method Invoke</description>
<parameter name="ServiceClass">axis2.service.server.ComplexTypeService</parameter>
<operation name="uploadImageWithByte">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getMDArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getDataForm">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getDataFormBytes">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="TbThresholdSettingJdbcService">
<description>Multy Method Invoke With Parameters</description>
<parameter name="ServiceClass">axis2.service.server.TbThresholdSettingJdbcService</parameter>
<operation name="getArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getMDArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getXmlData">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="SpringJdbcService">
<description>Multy Method Invoke With Parameters For Spring</description>
<!-- <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter> -->
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name="SpringBeanName">springAwareService</parameter>
<!--
<parameter name="ServiceClass">axis2.service.springjdbc.SpringJdbcService</parameter>
<parameter name="useOriginalwsdl">false</parameter>
<parameter name="modifyUserWSDLPortAddress">true</parameter>
<operation name="getArray">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
-->
<operation name="getArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getMDArray">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getXmlData">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
【说明】:把工程发布到Tomcat下,启动Tomcat并在IE地址栏输入如下访问地址:
http://localhost:8081/TestAxis2Ws/services/SpringJdbcService?wsdl
然后运行如下客户端代码即可。
SpringJdbcClient.java代码:
package axis2.service.domain;
import java.util.Iterator;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class SpringJdbcClient {
static Logger logger = Logger.getLogger(SpringJdbcClient.class);
/**
* @param args
*/
public static void main(String[] args) {
RPCServiceClient serviceClient = null;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8081/TestAxis2Ws/services/SpringJdbcService");
options.setTo(targetEPR);
System.out.println("调用第一个方法...");
//下面的代码调用了getArray方法,并返回一维String数组
String cszjfx = "CSZJFX";
Object[] opAddEntryArgs = new Object[]{cszjfx};
Class[] classes = new Class[]{String[].class};
QName opAddEntry = new QName("http://server.service.axis2", "getArray");
String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
for (String s : strArray)
System.out.print(s + " ");
System.out.println();
System.out.println("调用第二个方法...");
//下面的代码调用了getMDArray方法,并返回一维String数组
opAddEntryArgs = new Object[]{"DEVICE"};
opAddEntry = new QName("http://server.service.axis2", "getMDArray");
strArray = (String[]) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, new Class[]{String[].class})[0];
for (String s : strArray){
String[] array = StringUtils.split(s, ",");
for(String ss: array)
System.out.print("<" + ss + "> ");
System.out.println();
}
System.out.println();
System.out.println("调用第三个方法...");
//下面的代码调用了getXmlData方法,并返回一个XML格式的字符串
opAddEntryArgs = new Object[]{"CSZJFX"};
opAddEntry = new QName("http://server.service.axis2", "getXmlData");
String strXml = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, new Class[]{String.class})[0];
System.out.println(strXml);
Document document = DocumentHelper.parseText(strXml);
Element rootElm = document.getRootElement();
StringBuffer sb = new StringBuffer();
for(Iterator iter = rootElm.elementIterator(); iter.hasNext();){
Element element = (Element) iter.next();
System.out.println(element.getText());
}
} catch (AxisFault e) {
logger.error(e.getMessage());
System.out.println(e.getMessage());
e.printStackTrace();
} catch (DocumentException e) {
logger.error(e.getMessage());
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
大功告成,