springmvc三大件的配置
每次请求过来的时候,springmvc就要遍历这个文件,找找看,是哪个类型的HandlerMapping, 是哪个类型的HandlerAdapter。这样,执行效率太低了。我们可以在配置文件中配置好。
SpringMvc.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 注解形式的处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<!-- 注解形式的处理器适配器 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
</beans>
点进去看源码:
过时了。那我们找新的。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 配置最新版的注解的处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置最新版的注解的处理器适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
</beans>
那每次spring出了新的处理器映射器和处理器适配器,那么,我们都要去改啊。这样不行。配置注解驱动。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 注解驱动。
作用:替我们配置最新版的注解的处理器映射器和处理器适配器
-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
再来配视图解析器
我们当前还没有配置视图解析器,也能正常运行,是因为用了默认的解析器,jsp的解析器。但是,我们面临一个问题:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 注解驱动。 作用:替我们配置最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器.
作用:在Controller中指定页面路径的时候就不用写页面的完整路径名称了
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径= 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
总结:
在springmvc的核心配置文件中做3件事:
- 配置注解扫描
- 配置注解驱动
- 配置视图解析器