首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

小弟我不熟悉spring+struts+ibatis配置文件

2012-08-29 
我不熟悉spring+struts+ibatis配置文件对ssi不熟悉导致经常纠结于各种配置文件,对其如何整合也似懂非懂。干

我不熟悉spring+struts+ibatis配置文件
对ssi不熟悉导致经常纠结于各种配置文件,对其如何整合也似懂非懂。干脆来写写罢(主要写我不熟悉的,路过的同志可能不适合阅读此文)

一、web.xml
1、contextConfigLocation:Spring容器启动时需要加载Spring的配置文件,默认是/WEB-INF目录下的applicationContext.xml文件。当然也可以放在classpath下,可以包括多个spring配置文件----这就得依靠contextConfigLocation。

<context-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/applicationContext.xml</param-value>      </context-param> 

(开头的斜杠可有可无,可以是/WEB-INF/xxx.xml,也可以是WEB-INF/xxx.xml)
如果web.xml中没有配置context-param,spring的配置就像如上这段代码示例一下,自动去WEB-INF目录下寻找applicationContext.xml。此时,如果你修改applicationContext.xml的名称,或者移除它,再启动服务器,你会得到如下异常信息:
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

这证实了其默认配置。默认配置情况下spring只会去WEB-INF目录下寻找配置文件,而不会去classpath下寻找。
如果我们不想将配置文件放在WEB-INF目录下呢?开发中经常在src下面创建一个config目录,用于存放配置文件。此时,对应的param-value改为:classpath:config/applicationContext.xml。
一定要加上classpath,这告诉spring去classes目录下的config目录下面寻找配置文件。如果不加呢?则去war包的根目录下面寻找配置文件(此处要多多熟悉war包结构)。

怎样加载多个spring配置文件呢?很简单,param-value对应多个值,并且以逗号、空格、分号分隔配置文件字符串。例如:
<context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml,/WEB-INF/a.xml;classpath:c.xml</param-value>      </context-param> 

这样就加载了三个spring的配置文件。
也可以使用通配符,例如:加载classpath下config目录中所有applicationContext-开头的xml文件:
<context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:config/applicationContext-*.xml</param-value>      </context-param> 


2.如何启动Spring容器。
两种方法,一种以listener启动,一种以load-on-startup Servlet。先看第一种:
<listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>

第二种:
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet>

二选一,区别不大,都依赖于ContextLoader。

3.整合struts
也不难,通过配置servlet进行:
<filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping>


二。spring配置文件
可以在spring的配置文件中导入其他的spring配置文件:
<import resource="applicationContext-2.xml"/>

这里的import是基于相对路径。

加载properties文件:
<bean id="propertyConfigurer" name="code"><bean id="sqlMapClient" value="WEB-INF/sql-map-config.xml"/></bean>

应用程序会去WEB-INF目录下寻找sql-map-config.xml文件。

四.sql-map-config.xml
ibatis本身的配置不难:
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />

引入properties文件:
<properties resource="database.properties"/>

可以导入其他的ibatis映射文件:
<sqlMap resource="com/ailk/liaofeng/user/dao/ibatis/SystemUser.xml" />

ibatis有一套自己的配置我就不讲了,这里主要讲如何跟spring整合,所以,dataSource,transactionManager等对象都会使用spring进行配置。(ibatis配置文件请参考http://imticg.iteye.com/blog/216080)

四.struts.xml
<constant name="struts.objectFactory" value="spring"/><include file="newstruts.xml"/>

第一句,constant配置struts的常量(也可在struts.properties)文件中配置),将struts的对象工厂托由spring管理。
第二句,导入其他的struts配置文件。
其他详细配置参考:http://hanxin0311.iteye.com/blog/206675
我可以转走吗?谢谢 2 楼 ps329795485 2012-01-04   谢谢,在这里找到了我想要的答案。
刚开始我不知道是如何加载applicationContext.xml文件的,我什么都没配置,原来是默认在WEB-INF下加载的。

热点排行