发表于: 2017-05-20 23:26:40

6 1250


今天完成的事:

将昨天的问题解决了

经过不停的捣鼓觉得通过RMI将service与web分离后,service的spring配置文件只要一个就好了,如果有用到mybatis,把他们放到一起就可以了:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">
   <!--自动扫描-->
   <context:component-scan base-package="com.jnshu.*"/>
   <!--第一种方式:加载一个properties文件-->
   <bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location" value="classpath:jdbc.properties"/>
   </bean>
   <!--配置数据源-->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close">
       <property name="driverClassName" value="${driverClass}"/>
       <property name="url" value="${jdbcUrl}"/>
       <property name="username" value="${serName}"/>
       <property name="password" value="${password}"/>
       <!--初始化连接池大小-->
       <property name="initialSize" value="${initialSize}"/>
       <!--连接池最大数量-->
       <property name="maxActive" value="${maxActive}"/>
       <!--连接池最大空闲-->
       <property name="maxIdle" value="${maxIdle}"/>
       <!--连接池最小空闲-->
       <property name="minIdle" value="${minIdle}"/>
       <!--获取连接最大等待时间-->
       <property name="maxWait" value="${maxWait}"/>
   </bean>
   <!--mybatisspring完美整合,不需要mybatis的配置映射文件-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="mapperLocations" value="classpath:mapping/*.xml"/>
   </bean>

   <!--DAO接口所在的包名,Spring会自动查找其下的类-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.jnshu.dao"/>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
   </bean>
   <!--(事物管理器)transaction manager,use JtaTransactionManager for global tx-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   </bean>
   <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
   <tx:annotation-driven transaction-manager="transactionManager"/>

   <!--spring RMI service(部分)  开放服务和端口-->
   <bean id="studentService" class="com.jnshu.impl.StudentServiceImpl"/>
   <bean id="baseServiceExporter1" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="studentService"></property>
       <!-- 配置服务名称-->
       <property name="serviceName" value="studentService"></property>
       <!-- 配置服务接口-->
       <property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
       <!-- 配置服务端口-->
       <property name="registryPort" value="7777"></property>
   </bean>

   <bean id="baseRmiService" class="com.jnshu.impl.BaseServiceImpl"/>
   <bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="baseRmiService"></property>
       <!-- 配置服务名称-->
       <property name="serviceName" value="baseService"></property>
       <!-- 配置服务接口-->
       <property name="serviceInterface" value="com.jnshu.service.BaseService"></property>
       <!-- 配置服务端口-->
       <property name="registryPort" value="7777"></property>
   </bean>

</beans>

用main方法测试(加载) 

 public static void main(String[] args) {
// TODO Auto-generated method stub
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
//        BaseService baseService = (BaseService) context.getBean("baseRmiService");

       StudentService studentService = (StudentService) context.getBean("studentService");
       System.out.println("测试调用====ID=8====" + studentService.getStudentById(8));

如果是web项目通过Tomcat加载需在web.xml中配置

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring-mybatis.xml</param-value>
</context-param>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

web(访问)端配置:

<beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
   <context:component-scan base-package="com.jnshu.*"/>
   <!--<context:component-scan/>-->
   <bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl" value="rmi://127.0.0.1:7777/baseService"></property>
       <property name="serviceInterface" value="com.jnshu.service.BaseService"></property>
   </bean>
   <bean id="studentService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl" value="rmi://127.0.0.1:7777/studentService"></property>
       <property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
   </bean>
</beans>

访问(web)端main方法测试

public static void main (String [] args)throws NotBoundException,
           java.net.MalformedURLException,
           RemoteException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
//        BaseService baseService = (BaseService) context.getBean("baseService");
       StudentService studentService= (StudentService) context.getBean("studentService");
//        User user = new User();
//        System.out.println(baseService.getHelloword("zhangs9999999999"));
//        user.setName("bocosoftbababab");
//        user.setAge(33);
       try {
//            System.out.println(baseService.getUser(user));
           System.out.println(studentService.getStudentById(9));
       } catch (Exception e) {
// TODO Auto-generated catch block
           e.printStackTrace();
       }
}

遇到的问题:无

收获:web与service分离

明天计划的事:准备小课堂,讲小课堂,如果时间充足的话,将web的端的页面和控制器加上并在服务器上测试。

总结:好好学习


返回列表 返回列表
评论

    分享到