<servlet> <servlet-name>application</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="userService" class="org.felix.service.impl.UserServiceImpl" /> <!-- 基於Url映射方式,這個配置,就是把userService接口,提供給遠程調用 --> <bean id="httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="userService"/> <property name="serviceInterface" value="org.felix.service.UserService"/> </bean> <!-- 遠程服務的URL --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test">httpService</prop> </props> </property> </bean> </beans>
package org.felix.model; import java.io.Serializable; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }UserService接口
package org.felix.service; import org.felix.model.User; public interface UserService { void add(User u); }UserService接口的實現類UserServiceImpl
package org.felix.service.impl; import org.felix.model.User; import org.felix.service.UserService; public class UserServiceImpl implements UserService { @Override public void add(User u) { System.out.println("add user[" + u.getName() + "]"); } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="httpTestService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8080/felix_0400_SpringHttp_Server/test</value> </property> <property name="serviceInterface"> <value>org.felix.service.UserService</value> </property> </bean> </beans>
import org.felix.model.User; import org.felix.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpringHttp { private static ApplicationContext context = new ClassPathXmlApplicationContext("application-servlet.xml"); public static UserService getUserService(){ return (UserService)context.getBean("httpTestService"); } public static void main(String[] args) { User user = new User(); user.setName("felix"); user.setPassword("felix"); getUserService().add(user); } }
參考文章 java