Spring根据逻辑分层配置
背景:
通常简单的项目 ,一个spring.xml的配置文件即可搞定,但是当项目足够大的时候,我们如果将web ,service ,dao这些层的配置都放在spring.xml中,那这个配置文件将相当庞大和混乱,不易维护。
解决:
一个比较好的做法是将配置文件分为三层写三个配置文件(spring-web.xml , spring-service.xml, spring-dao.xml)配置方法如下:
web.xml:
[html]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
spring-web.xml:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="messageServlet" class="com.downjoy.app.messageboard.ui.web.wml.MessageCtrlServlet">
<property name="messageService" ref="messageService"/>
</bean>
<bean id="userServlet" class="com.downjoy.app.messageboard.ui.web.wml.UserCtrlServlet">
<property name="userService" ref="userService"/>
</bean>
</beans>
spring-service.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="messageService" class="com.downjoy.app.messageboard.common.service.MessageServiceImpl">
<property name="messageDAO" ref="messageDAO"/>
</bean>
<bean id="userService" class="com.downjoy.app.messageboard.common.service.UserServiceImple">
<property name="userDAO" ref="userDAO"/>
</bean>
</beans>
pring-dao.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="messageDAO" class="com.downjoy.app.messageboard.common.dao.ibatis.MessageDAOImpl"/>
<bean id="userDAO" class="com.downjoy.app.messageboard.common.dao.ibatis.UserDAOImpl"/>
</beans>
当然还有一种方式就是在各个子配置文件中,将需要的父配置文件import进来,这里不具体展开。
补充:软件开发 , Java ,