2.接å£å®žçް
package demo.spring;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
package demo.spring.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.HelloWorld;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
String response = client.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}
client-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="demo.spring.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld"/>
<property name="address" value="http://localhost:8080/example/jws/HelloWorld"/>
</bean>
</beans>
<!-- END SNIPPET: beans -->
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.HelloWorldImpl"
address="/HelloWorld" />
</beans>
<!-- END SNIPPET: beans -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>example</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>/jws/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
æ–°å¾ä¸€ä¸ªå¯¹è±¡Account,ä¾›webservices调用
package webServices;
public class Account {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
æ–°å¾ä¸€ä¸ªæŽ¥å?br /> package webServices;
public interface MathService {
Account sayHello(Account account);
}
实现¾c?br /> package webServices;
public class MathServiceImpl implements MathService{
@Override
public Account sayHello(Account account) {
account.setName("hello"+account.getName());
return account;
}
}
æ–°å¾WEB-INF/spring.xml,一个简å•beané…ç½®
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd">
<bean id="mathBean" class="webServices.MathServiceImpl"/>
</beans>
æ–°å¾WEB-INF/xfire-servlet.xml,webserviceé…置相关信æ¯
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd">
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/MathService">
<ref bean="math" />
</entry>
</map>
</property>
</bean>
<bean id="math"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>
<property name="xfire">
<ref bean="xfire" />
</property>
<!-- springé…置实现接壾c?->
<property name="serviceBean">
<ref bean="mathBean" />
</property>
<!-- 接壾c?->
<property name="serviceClass">
<value>webServices.MathService</value>
</property>
</bean>
</beans>
修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring.xml
classpath:/org/codehaus/xfire/spring/xfire.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
å¯åЍweb应用讉K—®http://localhost:8080/test/MathService?wsdlå¯ä»¥çœ‹è§webserviceé…置信æ¯
JAVA客户端测�br />
package client;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import webServices.Account;
import webServices.MathService;
public class Client {
/** *//**
* @param args
*/
public static void main(String[] args){
String serviceURL="http://localhost:8080/test/MathService";
Service serviceModel = new ObjectServiceFactory().create(MathService.class,null,serviceURL,null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
MathService service = null;
try {
service = (MathService) serviceFactory.create(serviceModel, serviceURL);
Account account=new Account();
account.setName("example");
System.out.println(service.sayHello(account).getName());
} catch (MalformedURLException e){
e.printStackTrace();
}
}
}
vs2005里é¢è°ƒç”¨å¢žåŠ web引用
导入相关¾c?直接new ç”?
import example.localhost.*;
AccountDao accountService = new AccountDao();
textBox3.set_Text(accountService.sayHello("hhhhh"));
package cn.ynzc.certificateQuery.hibernate;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.ynzc.certificateQuery.dao.Dao;
public class DaoImpl extends HibernateDaoSupport implements Dao {
public List findAllBy(final Class clazz, final String name,
final Object value) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clazz);
criteria.add(Restrictions.eq(name, value));
return criteria.list();
}
});
}
public List findAllBy(final Class clazz, final Map filter, final Map sort,
final int pageNo, final int pageSize) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clazz);
criteriaFilter(criteria, filter);
criteriaSort(criteria, sort);
if (pageNo != -1) {
criteria.setFirstResult(pageNo - 1 > 0 ? (pageNo - 1)
* pageSize : 0);
criteria.setMaxResults(pageSize);
}
return criteria.list();
}
});
}
private void criteriaFilter(Criteria criteria, Map filter) {
if (MapUtils.isNotEmpty(filter)) {
for (Iterator iterator = filter.keySet().iterator(); iterator
.hasNext();) {
String fieldName = (String) iterator.next();
Object value = filter.get(fieldName);
if (fieldName.indexOf('.') > 0) {
String[] sArray = StringUtils.split(fieldName, '.');
for (int i = 0; i < sArray.length; i++) {
if (i == 0)
criteria.createAlias(sArray[i], "alias_"
+ sArray[i]);
else if (i > 0 && i < sArray.length - 1)
criteria.createAlias("alias_" + sArray[i - 1] + "."
+ sArray[i], "alias_" + sArray[i]);
else
fieldName = "alias_" + sArray[i - 1] + "."
+ sArray[i];
}
}
criteria.add(value == null ? Restrictions.isNull(fieldName)
: Restrictions.eq(fieldName, value));
}
}
}
private void criteriaSort(Criteria criteria, Map sort) {
if (MapUtils.isNotEmpty(sort)) {
for (Iterator iterator = sort.keySet().iterator(); iterator
.hasNext();) {
String key = (String) iterator.next();
criteria.addOrder(StringUtils.equalsIgnoreCase("asc",
(String) sort.get(key)) ? Order.asc(key) : Order
.desc(key));
}
}
}
public Object findBy(final Class clazz, final String name,
final Object value) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clazz);
criteria.add(Restrictions.eq(name, value));
List list = criteria.list();
return list == null || list.isEmpty() ? null : list.get(0);
}
});
}
public Object getObject(Class clazz, Serializable id) {
return getHibernateTemplate().get(clazz, id);
}
public List getObjects(Class clazz) {
return getHibernateTemplate().loadAll(clazz);
}
public void removeObject(Object object) {
getHibernateTemplate().delete(object);
}
public void removeObject(Class clazz, Serializable id) {
getHibernateTemplate().delete(getObject(clazz, id));
}
public void removeObject(Collection collection) {
getHibernateTemplate().deleteAll(collection);
}
public Serializable saveObject(Object object) {
return getHibernateTemplate().save(object);
}
public int total(final Class clazz, final Map filter) {
return ((Number) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clazz);
criteriaFilter(criteria, filter);
criteria.setProjection(Projections.rowCount());
criteria.setCacheable(true);
return criteria.uniqueResult();
}
})).intValue();
}
public void updateObject(Object object) {
getHibernateTemplate().update(object);
}
}
ProxyPassReverse /app1 http://192.168.100.201:7001