struts2与spring集成启动的那些事

32 篇文章 0 订阅
24 篇文章 0 订阅

 在struts2与spring集成的系统中struts2与spring仍然是相关独立的框架系统,它们的联系也是非常有限的.对于WEB程序所有框架启动当然都是在web.xml这个文件中,至少源头是从这里开始的.下面就看一个最简单的集成配置,struts2.2与spring2.5的集成配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/**/applicationContext*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
		<init-param>
			<!-- 这里是strut2配置文件入口,针对FilterDispatcher -->
			<param-name>config</param-name>
			<!-- 这里不能使用通配符,定制配置文件名称可以在这里完成,无使用默认的struts.xml文件名 -->
			<param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
		</init-param>
	</filter>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

从上面的配置可以看出spring框架的入口是org.springframework.web.context.ContextLoaderListener监听器入配置参数为contextConfigLocation,它们所要做的唯一一件事情就是将spring容器初化,并初始化的容器以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性放置在WEB容器的Application范围中

struts2的框架入口是org.apache.struts2.dispatcher.FilterDispatcher(关于Servlet的启动这里就不说了,目标都差不多)的过滤器它实际是一个控制器,这是由框架自身的特点所决定的.这个过滤器有一个非常重的参数就是config,它是struts2程序入口配置文件的参数通常这是个参数是不配置的默认值是struts-default.xml,struts-plugin.xml,struts.xml这三个文件,它们都必须是类资源文件,第一个文件是struts2的默认配置文件,第二个文件是一个泛指的代表,struts2有一个插件体系每一个插件都有一个struts-plugin.xml第一个配置文件就是所有加载的插件配置文件,这两类文件都是在jar包中的,第三个配置文件struts2默认action配置文件.因为struts2所业务配置文件有一套包含解析系统,如果你有很多个业务配置文件,你可在struts.xml文件中包所有的业务配置文件,正是因为有包含解析机制,所以通常情况下你不需要对confg配置参数进行定制,当然如果你不喜欢你的配置文件包配置的根目录下那么你就有必要进行定制了,但有一点你必须知道定制最好带上struts-default.xml,struts-plugin.xml这两个在jar包中根目标下的配置文件因为它们会为你带来很多便利.例如你可如下配置:struts-default.xml,struts-plugin.xml,com/config/my-struts.xml但这个地方有一个非常值得说明一下的就是在配置的前后不要换行处理,因为这会导致文件名称的解析失败,这在其它配置文件的出现的地方也是要特别注意的例如log4jConfigLocation, contextConfigLocation等配置项

下面是一个启动的日志范例:

[2011/08/02 10:02:32.531] 'select-manager' requires Resin Professional.  See http://www.caucho.com for information and licensing.
[2011/08/02 10:02:32.531] Server[id=,cluster=] starting
[2011/08/02 10:02:32.531]
[2011/08/02 10:02:32.531] Windows XP 5.1 x86
[2011/08/02 10:02:32.531] Java 1.5.0_11-b03, 32, mixed mode, GBK, zh, Sun Microsystems Inc.
[2011/08/02 10:02:32.531] user.name: Administrator
[2011/08/02 10:02:32.531] resin.home = D:\ProgramFiles\dev\webserver\resin-3.1.1
[2011/08/02 10:02:32.531] root.directory = D:\ProgramFiles\dev\webserver\resin-3.1.1
[2011/08/02 10:02:32.531] resin.conf = /D:/ProgramFiles/dev/webserver/resin-3.1.1/conf/myconfig/resin(risen-test).xml
[2011/08/02 10:02:32.546]
[2011/08/02 10:02:32.562] hmux listening to localhost:6800
[2011/08/02 10:02:32.578] http listening to *:9080
[2011/08/02 10:02:32.593] Host[] starting
[2011/08/02 10:02:32.968] WebApp[http://localhost:9080/test] starting
[2011/08/02 10:02:33.125] Initializing Spring root WebApplicationContext
[2011/08/02 10:02:33.125] Root WebApplicationContext: initialization started
[2011/08/02 10:02:33.265] Refreshing org.springframework.web.context.support.XmlWebApplicationContext@1579a30: display name [Root WebApplicationContext]; startup date [Tue Aug 02 10:02:33 CST 2011]; root of context hierarchy
[2011/08/02 10:02:33.546] Loading XML bean definitions from file [E:\我的地盘\工作空间\Eclipse工作空间\MyEclipse 6.5_默认\risen-test\WebRoot\WEB-INF\classes\com\risen\core\config\applicationContext-pub.xml]
[2011/08/02 10:02:33.593] Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@1579a30]:org.springframework.beans.factory.support.DefaultListableBeanFactory@15f1f9c
[2011/08/02 10:02:33.734] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15f1f9c: defining beans []; root of factory hierarchy
[2011/08/02 10:02:33.734] Root WebApplicationContext: initialization completed in 609 ms
[2011/08/02 10:02:34.031] Unable to locate configuration files of the name config/default.xml, skipping
[2011/08/02 10:02:34.031] Unable to locate configuration files of the name config/module-pub.xml, skipping
[2011/08/02 10:02:34.046] Parsing configuration file [com/risen/core/config/struts.xml]
[2011/08/02 10:02:34.109] Parsing configuration file [struts-default.xml]
[2011/08/02 10:02:34.546] Parsing configuration file [struts-plugin.xml]
[2011/08/02 10:02:34.921] Initializing Struts-Spring integration...
[2011/08/02 10:02:34.921] Setting autowire strategy to name
[2011/08/02 10:02:34.921] ... initialized Struts-Spring integration successfully
[2011/08/02 10:02:36.000] WebApp[http://localhost:9080] starting
[2011/08/02 10:02:36.000] Resin started in 4593ms

从上面的日志你可以看到spring容器启动在先,struts2的启动在后,这是一个必然的,因为它们的入口机制不同前者是监听器,后者是过滤器.注意struts2 配置文件没有先后顺序,也就是文件之间的依赖关系是在后期计算出来的,无需要在配置的时候进行体现,这是struts2配置的一个特点,它分为静态配置与动态配置,表态配置就是文件原始信息,而动态配置是根据静态配置以及规则计算出来的配置最终程序招待所需要的,这种机制可以保证文件与文件之间可以有依赖,但可以并不体现在它们的载入的先后次序上.

 注意:struts2在xml配置文件内部通常include标签进行包含的文件之间没有先后次序它们之间的依赖关系可以不必体现,但是在过滤器中配置文件序列,是有先后次序的,通常越基础的配置放在最前面,一般的配置为struts-default.xml,struts-plugin.xml,com/config/my-struts.xml,而不要写成com/config/my-struts.xml,struts-default.xml,struts-plugin.xml这样通常解析会出错,my-struts.xml通常会对后面的配置有依赖

另外关于struts2配置文件的加载实际还有一些属性文件,并且与struts2的action配置文件文件中的组件及其它参数是重复的,这就有一个优先级的问题,这个问题在有篇转载的文件已经有说明它是说明了常量配置的优先级:

struts2的常量配置以第一个位置为准

优先级为:

1、struts-default.xml

2、struts-plugin.xml

3、struts.xml

4、struts.properties

5、web.xml

关于配置优先可以看Dispatch过滤器的一段代码:

   public void init() {

    	if (configurationManager == null) {
    		configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
    	}

        try {
            init_DefaultProperties(); // [1]
            init_TraditionalXmlConfigurations(); // [2]
            init_LegacyStrutsProperties(); // [3]
            init_CustomConfigurationProviders(); // [5]
            init_FilterInitParameters() ; // [6]
            init_AliasStandardObjects() ; // [7]

            Container container = init_PreloadConfiguration();
            container.inject(this);
            init_CheckConfigurationReloading(container);
            init_CheckWebLogicWorkaround(container);

            if (!dispatcherListeners.isEmpty()) {
                for (DispatcherListener l : dispatcherListeners) {
                    l.dispatcherInitialized(this);
                }
            }
        } catch (Exception ex) {
            if (LOG.isErrorEnabled())
                LOG.error("Dispatcher initialization failed", ex);
            throw new StrutsException(ex);
        }
    }


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值