发表于: 2017-11-28 22:40:47

1 588


今天完成的内容:

(1)准备小课堂——web.xml的配置。

web.xml加载过程:

当我们去启动一个WEB项目时,容器包括(JBoss、Tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。

启动WEB项目的时候,容器首先会去它的配置文件web.xml读取两个节点:  

<listener></listener>和<context-param></context-param>。

紧接着,容器创建一个ServletContext(application),这个WEB项目所有部分都将共享这个上下文。

容器以<context-param></context-param>的name作为键,value作为值,将其转化为键值对,存入ServletContext。

容器创建<listener></listener>中的类实例,根据配置的class类路径<listener-class>来创建监听,在监听中会有contextInitialized(ServletContextEvent args)初始化方法,启动Web应用时,系统调用Listener的该方法,在这个方法中获得:

ServletContext application =ServletContextEvent.getServletContext();

context-param的值= application.getInitParameter("context-param的键");

得到这个context-param的值之后,就可以做一些操作了。

然后,容器会读取<filter></filter>,根据指定的类路径来实例化过滤器。

以上都是在WEB项目还没有完全启动起来的时候就已经完成了的工作。如果系统中有Servlet,则Servlet是在第一次发起请求的时候被实例化的,而且一般不会被容器销毁,它可以服务于多个用户的请求。所以,Servlet的初始化都要比上面提到的那几个要迟。

总的来说,web.xml的加载顺序是: <context-param>-> <listener> -> <filter> -> <servlet>。其中,如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载。

对于某类元素而言,与它们出现的顺序是有关的。以<filter>为例,web.xml中当然可以定义多个<filter>,与<filter>相关的一个元素是<filter-mapping>,注意,对于拥有相同<filter-name>的<filter>和<filter-mapping>元素而言,<filter-mapping>必须出现在<filter>之后,否则当解析到<filter-mapping>时,它所对应的<filter-name>还未定义。web容器启动初始化每个<filter>时,按照<filter>出现的顺序来初始化的,当请求资源匹配多个<filter-mapping>时,<filter>拦截资源是按照<filter-mapping>元素出现的顺序来依次调用doFilter()方法的。<servlet>同<filter>类似。

(2)将任务代码中的studentservice分离。

服务端,主要是配置文件:

<!-- RMI服务端配置 -->
<bean id="studentRMIService"  class="com.jnshu.service.Impl.StudentServiceImpl"></bean>

<bean  id="ServiceExporter"  class="org.springframework.remoting.rmi.RmiServiceExporter">
   <property name="serviceName" value="StudentService"></property>
   <property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
   <property name="registryPort" value="9999"></property>
</bean>

客户端配置文件:

<!-- rmi客户端 -->
<bean id="RMIClient"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
   <property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
   <property name="serviceUrl" value="rmi://127.0.0.1:9999/StudentService"></property>
   <property name="refreshStubOnConnectFailure" value="true"></property>
   <property name="lookupStubOnStartup" value="false"></property>
</bean>

客户端启动:

import com.jnshu.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientTest {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-mvc.xml");
       StudentService studentService = applicationContext.getBean("RMIClient", StudentService.class);
       System.out.println("Server start...");
   }
}

运行:


出错了,应该是Dao的注入问题,暂时不知道怎么解决,明天查一下。

明天的计划:跑通rmi studentservice,web调用它,还有小课堂。

遇到的问题:bug。

收获:以上。

禅道:http://task.ptteng.com/zentao/task-view-10713.html


返回列表 返回列表
评论

    分享到