把spring配置分解按模块分解,比把成百个bean写到一个文件中要清楚的多,找bean修改也方便的多,同时减少团队开发修改一个文件产生的冲突。
web中的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
spring主文件spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="springUtil" class="com.cplat.util.SpringUtil"></bean>
<import resource="classpath:com/cplat/**/spring-*.xml" />
</beans>
模块中的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="userDao" class="com.cplat.modules.user.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.cplat.modules.user.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.cplat.modules.user.UserAction">
<property name="userService" ref="userService"></property>
<property name="roleService" ref="roleService"></property>
</bean>
</beans>
项目结构图