SpringMVC(三)----SpringMVC组件

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件事:

  1. 配置注解扫描
  2. 配置注解驱动
  3. 配置视图解析器


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值