Posted on 2010-01-27 16:33
瘋狂 閱讀(2507)
評論(0) 編輯 收藏 所屬分類:
spring
1,創(chuàng)建接口:
public interface HttpInvokerTestI {
public TestPo getTestPo(String desp);
}
實現(xiàn):
public class HttpInvokertestImpl implements HttpInvokerTestI {
@Override
public TestPo getTestPo(String desp) {
// TODO Auto-generated method stub
return new TestPo(desp);
}
}
配置:在web info下添加remote-servlet.xml
內(nèi)容:
<bean name="httpinvokertest" class="ztest.httpinvoke.HttpInvokertestImpl"/>
<bean name="/hit" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="httpinvokertest"/>
<property name="serviceInterface" value="ztest.httpinvoke.HttpInvokerTestI"/>
</bean>
添加spring.jar spring-webmvc.jar(配置dispacherServlet用)
web.xml 添加對context和servlet的支持:
<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>
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
客戶端配置:
<bean id="remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/ssh/remoting/hit" />
<property name="serviceInterface" value="com.s.httpinvoker.HttpInvokerTestI"/>
</bean>
測試類:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml");
HttpInvokerTestI httpInvokerTestI = (HttpInvokerTestI) context.getBean("remoteService");
System.out.println(httpInvokerTestI.getTestPo("dddd").getDesp());
}
完成。