Spring根据逻辑分层配置

2014-11-24 09:09:54 · 作者: · 浏览: 1

背景:

通常简单的项目 ,一个spring.xml的配置文件即可搞定,但是当项目足够大的时候,我们如果将web ,service ,dao这些层的配置都放在spring.xml中,那这个配置文件将相当庞大和混乱,不易维护。

解决:

一个比较好的做法是将配置文件分为三层写三个配置文件(spring-web.xml , spring-service.xml, spring-dao.xml)配置方法如下:

web.xml:
[html]

contextConfigLocation
classpath:spring-*.xml

spring-web.xml:

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">







spring-service.xml

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">







pring-dao.xml

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">




当然还有一种方式就是在各个子配置文件中,将需要的父配置文件import进来,这里不具体展开。