Web.xml内容解析:
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置文件加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<! -- 中间配置log4j -->
<! -- 配置Spring的用于初始化容器对象的监听器 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
当Web应用启动时,将执行以下操作:
①由Tomcat创建一个ServletContext,并自动载入<context-param>中的属性;
②ContextLoaderListener检测到ServletContext的创建,运行Spring的相关程序;
③Spring根据<context-param>中contextConfigLocation的指向,加载对应applicationContext.xml;
④Spring根据applicationContext.xml的内容创建一个BeanFactory实例,并放入ServletContext中。
简而言之,该配置的作用是:当Web应用启动时,Spring将自动创建一个BeanFactory实例,并放入ServletContext中。
ContextLoaderListener作用:在启动Web容器时,自动装配SpringapplicationContext.xml的配置信息。
在ContextLoaderListener中关联了ContextLoader这个类, ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成, ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory这样一来spring中的所有bean都由这个类来创建
public classClassPathXmlApplicationContext implements BeanFactory {
private Map<String, Object> beans = new HashMap<String,Object>();
public ClassPathXmlApplicationContext(String fileName) throws Exception{
SAXReader reader = new SAXReader();
Document document =reader.read(this.getClass().getClassLoader().getResourceAsStream(fileName));
List<Element> elements =document.selectNodes("/beans/bean");
for (Element e : elements) {
String id =e.attributeValue("id");
String value =e.attributeValue("class");
Object o = Class.forName(value).newInstance();
beans.put(id, o);
}
}
public Object getBean(String id) {
return beans.get(id);
}
}
public static void main(String[]args){
ApplicationContext ctx = newClassPathXmlApplicationContext("bean.xml");//读取bean.xml中的内容
Person p =ctx.getBean("person",Person.class);//创建bean的引用对象
p.info();
}
}
通过获得xml文件中bean的id和name生成一个Map,之后就可以使用beanfactory将对应id的类生成。
Spring由listener启动之后,读取Spring的配置文件applicationContext.xml。如有多个配置文件,会全部读取。注意,是“全部”读取,不管有多少个。
Spring的配置文件中,基本都是<bean>,一个bean对应一个对象。Spring在读取配置完毕后,就立刻实例化这些对象。这些对象是很早就被实例化好了的,并被保存起来。当程序需要使用这些对象时,则直接从内存中获取。Spring会把这些实例好了的对象保存在一个map 里,第一次用的时候从map里取,查找速度是很快的。且查找一次之后,程序使用的就是引用了。
整个bean的生成过程:
1:生成xml格式的配置文件
2:通过ResourceLoader将配置文件读入
3:通过BeanDefinitionReader将配置信息转换程BeanDefinition
4:在通过BeanFactory根据BeanDefinition信息创建bean实例
5:将bean实例放入SingletonBeanRegistry中以供使用
<!-- 字符集过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Struts2的核心的过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
</filter-mapping>
Struts2的工作机制
从上图可以看出,一个请求在Struts2框架中的处理大概分为以下几个步骤:
1、客户端初始化一个指向Servlet容器(例如Tomcat)的请求;
2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin);
3、接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请求是否需要调用某个Action;
4、如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy;
5、ActionProxy通过ConfigurationManager询问框架的配置文件,找到需要调用的Action类;
6、ActionProxy创建一个ActionInvocation的实例。
7、ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。
8、一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2框架中继承的标签。在这个过程中需要涉及到ActionMapper
<context-param>
<param-name>CROSS_ORIGIN</param-name>
<param-value>*</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>