Spring、SpringMVC的配置文件
本人是个菜鸟,只是写我遇到的,我觉得怎样怎样的,我个人怎么理解的(我感觉我撒也不会)。
问题阐述:为何会有两个配置文件 两个配置文件都配置了
<context:component-scan base-package=“com.hjcrm”>
web.xml是这样配置的(我直接取这一部分):
<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载springMVC配置文件-->
<servlet>
<servlet-name>crm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context-dispatcher.xml</param-value>
</init-param>
<!--启动容器,即加载配置文件(防止遗漏)-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--请求后缀-->
<servlet-mapping>
<servlet-name>crm</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
applicationContext.xml中的配置:
<context:component-scan base-package="com.hjcrm">
<!--将Controller的注解排除掉 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
context-dispatcher.xml的配置
<context:component-scan base-package="com.hjcrm.**.controller" >
<!--使用注解的包,包括子集-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
也是从别人的博客了解到,得到的结果是:
这是将他们分开配置,就好像解耦一样,让两个配置文件各司其职,各管各的模块,能够分工明确,context-dispathcer更是只处理controller的内容,对于代码的编写来说更加的好理解,更清晰!