关于使用DispatcherServlet加载spring流程中,binFactory 的设置
| 1 2 3 |
|
BeanFactoryAware、 BeanNameAware、ApplicationContextAware、ResourceLoaderAware、ServletContextAware
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
通过实现ApplicationContextAware获取spring上下文
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
其他Aware接口
| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
| 1 2 3 4 5 6 7 8 9 |
|
| 1 2 3 4 5 6 7 8 9 10 11 |
|
(1)监听器
ServletContext监听器javax.servlet.ServletContextListener
在Container 加载Web 应用程序时(例如启动 Container 之后),会呼叫contextInitialized() ,而当容器移除Web 应用程序时,会呼叫contextDestroyed () 方法
监听ServletContext对象的生命周期,实际上就是监听Web应用的生命周期
<context-param>
<param-name>propertiesConfigLocation</param-name>
<param-value>/props/cashier-web.properties,/props/cashier-error-code.properties</param-value>
</context-param>
<listener>
<listener-class>cn.com.xmh.frame.listener.PropertiesConfigListener</listener-class>
</listener>
监听器实现类PropertiesConfigListener中获取propertiesConfigLocation
propertiesConfigLocation = sce.getServletContext().getInitParameter("propertiesConfigLocation").trim();
读取配置文件放至 ApplicationCache.getInstance().put(key, value); 通过监听器监听服务启动,加载配置文件到ApplicationCache 自定义缓存类(非spring)
1、org.springframework.web.util.IntrospectorCleanupListener
2、servlet和Filter初始化前和销毁后,都会给实现了servletContextListener接口的监听器发出相应的通知
3、当Servlet容器启动或终止Web应用时,触发ServletContextListener中两个接口
(2)xml文件加载
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml,/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而这个一般是采用非spring mvc架构,如使用struts之类而又想引入spring才添加的,这个是用来加载Application Context。
如果直接采用SpringMVC,只需要把所有相关配置放到xxx-servlet.xml中就OK了
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1、ContextLoaderListener实现了ServletContextListener这个接口,所以服务启动后就会执行,ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成
2、ContextLoader中initWebApplicationContext的过程,方法名称即是其含义。方法中首先创建了WebApplicationContext,配置并且刷新实例化整个SpringApplicationContext中的Bean,放入servletContext中 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); 获取xml中contextConfigLocation的属性值
3、ContextLoaderListener类起着至关重要的作用。它读取web.xml中配置的context-param中的配置文件,提前在web容器初始化前准备业务对应的Application context;将创建好的Application context放置于ServletContext中,为springMVC部分的初始化做好准备
容器创建了Servlet实例后,它将调用实例的init(ServletConfig)方法初始化Servlet.该方法的参数ServletConfig对象包含了在WEB应用程序的部署描述文件中指定的初始化参数。在init(ServletConfig)调用完后,容器将调用init()方法,之后Servlet就被初始化了
调用init(ServletConfig config) 进行初始化,ServletConfig封装了web.xml中<init-param>配置的初始化参数,它由Servlet容器创建,并通过该方法传递给当前serlvet
(3)拦截器
SpringMVC 中的Interceptor拦截器org.springframework.web.servlet.HandlerInterceptor 实现该接口
spring配置文件
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<bean class="cn.com.xmh.cashier.web.interceptor.CashierUserLoginInterceptor">
<property name="allowUrls">
<list>
<value>payController/404error.dhtml</value>
<value>payController/500error.dhtml</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
拦截器实现类中设置私有变量,preHandle实现该方法
private String[] allowUrls = null;
spring线程池
(4)properties配置文件后置处理
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。
在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改
| 1 2 3 4 5 6 7 8 |
|
继承类GroupPropertyPlaceholderConfigurer中获取配置
Properties mergeProperties = super.mergeProperties();
mergeProperties.put("runtimecashierdubbogroup", group);//“重新”设置配置信息
注释:每台服务器properties文件配置信息一致,根据不同服务来选取配置信息中对应的value(服务地址作为key),比如value为dubbo的group信息
(5)properties配置参数在xml文件中占位符
spring中通过${dubbo.run.redis}来获取.properties文件中dubbo.run.redis的值
<bean id="customPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<array>
<value>classpath:props/cashier-web.properties</value>
</array>
</property>
</bean>
关于ApplicationContextAware的实现类是如何设置上下文的(包含spring初始化)
1、spring初始化时会调用一下流程
| 1 2 |
|
2、使用DispatcherServlet加载spring
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3、使用ClassPathXmlApplicationContext加载spring
| 1 2 3 4 5 6 |
|
4、两种加载spring方式关联
| 1 2 3 4 5 6 7 |
|
5、ApplicationContext 的几种实现
XmlWebApplicationContext
专为web工程定制的方法,推荐Web项目中使用。例如:
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
FileSystemXmlApplicationContext
这个方法是从文件绝对路径加载配置文件,例如:
ApplicationContext ctx = new FileSystemXmlApplicationContext( "G:/Test/applicationcontext.xml ");
如果在参数中写的不是绝对路径,那么方法调用的时候也会默认用绝对路径来找,我测试的时候发现默认的绝对路径是eclipse所在的路径。
采用绝对路径的话,程序的灵活性就很差了,所以这个方法一般不推荐。(如果要使用classpath路径,需要加入前缀classpath: )
1万+

被折叠的 条评论
为什么被折叠?



