Spring的配置文件和SpringMvc的配置文件的区别
1、Spring的配置文件和SpringMVC配置文件的加载位置
都是在web.xml中指定的,一个是为了初始化跟容器,一个是为了初始化子容器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 根容器 -->
<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>
<!-- web 容器 子容器-->
<servlet>
<!-- 声明Dispatcher,告诉Web容器我们将使用springMVC的DispatcherServlet -->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet在加载时会需要SpringMVC的配置文件,因此需要声明该配置文件的位置。 -->
<!-- 如果不配置该项,则应用会默认的去WEB-INF下寻找名字为[servlet-name]-servlet.xml的文件。 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置url-pattern,设置为 / 拦截所有的URL请求,并映射到dispatcher -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/h1</url-pattern>
</servlet-mapping>
</web-app>
2、SpringMVC的配置文件作用
springMVC的配置文件主要和SpringMVC有关的配置,controller,servlet,viewResolver 等
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描包交给spirng的ioc来管理,让指定目录下的注解生效 -->
<context:component-scan base-package="com.kuang.controller"/>
<!-- 使用默认的 servlet handler, 过滤掉 .css .mp4 .js等的文件-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3、spring 的配置文件的作用
主要是实例化和初始化 service、dao 以及事务和别的东西集成的配置
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.kuang" >
<!-- 排除实现了Controller接口的类 Mycontroller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
浙公网安备 33010602011771号