发表于: 2017-09-16 22:54:54

1 722


今天做的事:

做了个RMI的小课堂,明天要讲。也不贴了。


然后就是给新来的试学师妹“科普”,大概讲了一些首席师兄该讲的。


然后晚上就是将SpringRMI整合到任务代码中,拆出来一个Service,目前有点问题,需要排一下bug


报错

 2017-09-16 23:00:47  [ RMI TCP Connection(2)-127.0.0.1:2306 ] - [ ERROR ]  Context initialization failed


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'oneController': Failed to introspect bean class [com.ptteng.Controller.OneController] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: com/ptteng/POJO/Student


这个类被我分离出去了,所有和Student相关的如StudentMapper、StudentService等都被我分离出去了;

然后通过在分离的server端配置SpringRMI的相关配置文件

<?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.5.xsd"

      default-autowire="byName">

   <bean id="rmiService" class="com.ptteng.ServiceImpl.StudentServiceImpl"/>

   <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <!--服务物件-->
       <property name="service">
           <ref bean="rmiService"/>
       </property>
       <!--服务名称-->
       <property name="serviceName">
           <value>rmiService</value>
       </property>
       <!--代理介面-->
       <property name="serviceInterface">
           <value>com.ptteng.Service.StudentService</value>
       </property>
       <!--设置注册端口-->
       <property name="registryPort" value="9999"></property>
       <!--设置服务端口-->
       <!--<property name="servicePort" value="8888" />-->
   </bean>

</beans>


然后在写个server启动

public class StuServer {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("spring/rmi-server.xml");
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       
       while (true){
              if(reader.readLine().equals("exit")){
              System.exit(0);
           }
}
}
}


一切都和之前的一样


然后客户端配置SpringRMI文件

<?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.5.xsd"

      default-autowire="byName">

   <bean id="rmiServiceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl">
           <value>rmi://localhost:9999/rmiService</value>
       </property>
       <property name="serviceInterface">
           <value>com.ptteng.Service.StudentService</value>
       </property>
   </bean>

</beans>


然后在Controller中获得相应的bean

ApplicationContext context = new ClassPathXmlApplicationContext("spring/rmi-client.xml");
StudentService sS = (StudentService) context.getBean("rmiServiceProxy");


就这里和之前的小demo不同,这里没有在main函数中,但是这个会有什么影响么?


目前报错找不到Student类,说明这个远程连接失败了,需要解决


明天计划:解决今天的bug


问题:远程连接失败


收获:准备了一下小课堂,对RMI理解加深



返回列表 返回列表
评论

    分享到