Background:
A simple project, a spring. the xml configuration file can be used, but when the project is large enough, we can put the configurations of web, service, and dao layers in spring. in xml, the configuration file will be very large and messy, and it is not easy to maintain.
Solution:
A good practice is to divide the configuration file into three layers write three configuration files (spring-web.xml, spring-service.xml, spring-dao.xml) configuration method is as follows:
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>
Of course, another way is to import the required parent configuration file in each sub-configuration file, which is not detailed here.