一界书生
天行键 君子以自强不息; 地势坤 君子以厚德载物
登录
注册
全站
当前博客
空间
博客
好友
相册
留言
使用import简化spring的配置文件
收藏
对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件。不过,分模块都是大多数人能想到的方法,但是,怎么分模块,那就是仁者见仁,智者见智了。我的策略是使用import。
基本代码格式如下
web.xml
......
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/spring-config/applicationContext.xml
</
param-value
>
</
context-param
>
......
web.xml文件中只需制定一个文件
在/WEB-INF/spring-config/目录下包含了所有spring配置文件,包括一个总配置文件,以及各个模块的配置文件
applicationContext.xml
<?
xml version="1.0" encoding="gb2312"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
import
resource
="CTIContext.xml"
/>
<
import
resource
="customerContext.xml"
/>
<
import
resource
="customerServingContext.xml"
/>
<
import
resource
="manageContext.xml"
/>
<
import
resource
="routineContext.xml"
/>
<
import
resource
="systemContext.xml"
/>
...........
包括数据源,以及事务的基本配置
...........
这时所有DAO的基类,各个模块的DAO都配置成继承这个类,就省去了在配置sessionFactory的麻烦
<!--
Base DAO
-->
<
bean
id
="baseDAO"
abstract
="true"
class
="com.mycompany.myproject.framework.BaseDAOImpl"
>
<
property
name
="sessionFactory"
>
<
ref
bean
="sessionFactory"
/>
</
property
>
</
bean
>
</
beans
>
applicationContext.xml文件中使用import的方式导入有模块配置文件,以后若有新模块的加入,那就可以简单修改这个文件了
system模块配置文件
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<!--
DAO
-->
<
bean
id
="userDAO"
parent
="baseDAO"
class
="com.mycompany.myproject.module.system.dao.UserDAOImpl"
>
<
constructor-arg
>
<
value
>
com.mycompany.myproject.domain.system.User
</
value
>
</
constructor-arg
>
</
bean
>
<
bean
id
="agentDAO"
parent
="baseDAO"
class
="com.mycompany.myproject.module.system.dao.AgentDAOImpl"
>
<
constructor-arg
>
<
value
>
com.mycompany.myproject.domain.system.Agent
</
value
>
</
constructor-arg
>
</
bean
>
<
bean
id
="agentGroupDAO"
parent
="baseDAO"
class
="com.mycompany.myproject.module.system.dao.AgentGroupDAOImpl"
>
<
constructor-arg
>
<
value
>
com.mycompany.myproject.domain.system.AgentGroup
</
value
>
</
constructor-arg
>
</
bean
>
<!--
Service
-->
<
bean
id
="userService"
parent
="baseTransactionProxy"
>
<
property
name
="target"
>
<
bean
class
="com.mycompany.myproject.module.system.service.UserServiceImpl"
>
<
property
name
="userDAO"
ref
="userDAO"
/>
</
bean
>
</
property
>
</
bean
>
</
beans
>
可以仔细看这个文件是怎么使用spring中abstract的方式申明继承关系的。
我看到有不少人采用spring自带的策略,把DAO与service层的配置分开,我认为这种策略不好,因为大多数时候,这两部分都是需要同时变动的,若放在两个文件中的话,那就会增加了每次修改配置文件的时间,而且,不利于重用。
若是结合我的上篇文章(关于spring单元测试的)的策略,那么这种方式就会发挥更大的威力
发表于 @
2006年09月14日 22:10:00
|
评论(
loading...
)
新一篇: hibernate的unique配置
|
旧一篇: spring中的单元测试的策略
用户操作
[即时聊天]
[发私信]
[加为好友]
johnny
订阅我的博客
johnny的公告
MSN:wangjian5748@hotmail.com
文章分类
ajax
DataBase
Eclipse
extremecomponents
Hibernate
J2EE
Java Article
others
Python
Reading
Regular Expression
Ruby
Software Engineering
Spring Framework
struts
struts2/webwork2
Version Control
Web(html,javascript,css,http)
weblogic
收藏
GUI
J2EE
others
web programming
weblogic server
Programming
bea weblogic6.1/5.1官方下载站点
Domain Driven Design
Extreme Programming Organization
IT Development and Test Site
obe下载网址(工作流)
The Language Guide
TheServerSide
从javascript语言本身谈项目实战
Python
ASPN Cookbook
core python programming
Dive Into Python
Jython
Main Python Site
mxODBC/mxDateTime,etc
Numerical Python
O'Reilly Network
O'Reilly Python Books
PyGame
PyOpenGL
Python Book Reviews
Python Starship
python.faqts
PythonWare
Red Robin Jython development plug-in for Eclipse
Vaults of Parnassus(Python Resources)
wxPython
Zope
肥肥世家
存档
2009年06月(1)
2009年04月(2)
2008年12月(1)
2008年11月(3)
2008年07月(1)
2008年06月(1)
2008年05月(2)
2008年04月(3)
2008年03月(3)
2008年02月(2)
2007年12月(7)
2007年10月(2)
2007年09月(2)
2007年08月(4)
2007年07月(1)
2007年06月(1)
2007年05月(6)
2007年04月(6)
2007年03月(10)
2007年01月(8)
2006年12月(3)
2006年11月(2)
2006年10月(5)
2006年09月(9)
2006年08月(3)
2006年07月(3)
2006年06月(4)
2006年05月(1)
2006年04月(8)
2006年03月(13)
2006年02月(12)
2006年01月(14)
2005年06月(1)
2004年07月(13)
2004年06月(2)
软件项目交易