【学习】springmvc之用@Controller定义控制器

一、简介

用@Controller注释表明一个特定的类充当控制器的角色。Spring不需要扩展任何控制器基类或引用Servlet API。但是,如果需要,仍然可以引用Servlet特定的功能。

用固定的@Controller注释充当注解类,这表明它的控制器作用。调度程序扫描这些注释类以获取映射方法并检测 @RequestMapping注释(后面学习)。
您可以使用调度程序上下文中的标准Spring bean定义来明确定义注释控制器bean。但是,@Controller注释的类也允许自动检测,与spring一般支持检测组件类路径和自动注册的bean定义他们。

为了使这样的注释控制器自动检测,你可以添加组件扫描您的配置。使用下面的XML片段中所示的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"
    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="org.springframework.samples.petclinic.web"/>

    <!-- ... -->

</beans>

所以根据扫描组件的内容我们可以将我们之前的XML配置修改一下

原配置

<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"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans 
	   					   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
						   http://www.springframework.org/schema/context 
						   http://www.springframework.org/schema/context/spring-context-4.3.xsd
		              	   http://www.springframework.org/schema/tx 
						   http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		              	   http://www.springframework.org/schema/mvc 
		              	   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
        
	<bean class="com.spring.mvc.FrontController"></bean>
	
	<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean>
</beans>

修改后的代码是

<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"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans 
	   					   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
						   http://www.springframework.org/schema/context 
						   http://www.springframework.org/schema/context/spring-context-4.3.xsd
		              	   http://www.springframework.org/schema/tx 
						   http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		              	   http://www.springframework.org/schema/mvc 
		              	   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
        
	<context:component-scan base-package="com.spring.mvc"></context:component-scan>
	<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean>
</beans>

二、context:component-scan的作用

用浏览器打开:http://www.springframework.org/schema/context/spring-context-4.3.xsd

使用组合件ctrl+F查找scan,标签整体如下

属性说明:

base-package:使用context:component-scan标签时,必须配置base-package属性,此属性是以package为单位,来扫描该package下所有class文件,并将其自动注册成bean

resource-pattern:定义扫描文件的格式,默认值是"**/*.class"。

use-default-filters:是否启用自动检测带有@Component, @Repository, @Service,  @Controller注释的类。默认为“true”。

annotation-config:指示是否应在启用隐式注释后处理程序。 默认为“true”。

name-generator : bean名称生成器,指示bean名称生成器完全限定为类名来命名检测单元

scope-resolver:范围解析器,范围解析器用类名来解决检测成分范围。

scoped-proxy:指示是否应该为检测到的组件生成代理,这可能是在使用代理样式时使用范围的必要条件。默认情况是不生成此类代理。

两个子标签:

include-filter:控制要扫描哪些类型的组件。 请注意,除了指定的默认过滤器之外,还将应用这些过滤器。 即使与默认过滤器不匹配(即不使用@Component注释),也将包含与给定过滤器匹配的指定基本包下的任何类型。

exclude-filter:控制哪些类型的组件不需要扫描


配置属性示例:

context:component-scan必须要配置base-package,有时候需要配置use-default-filters,其他的不一般都不配置采用默认

示例一,默认配置,base-package 如果多个,用“,”分隔

<context:component-scan base-package="com.spring.mvc"></context:component-scan>

配置use-default-filtrs=false时我们一般都是和子标签include-filter一起使用

示例二、配置include-filter,只扫描@Controller(此配置一般写在springmvc容器中)

<context:component-scan base-package="com.spring.mvc" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

示例三、配置exclude-filter,只不扫描@Controller(此配置一般写在主容器中)

<context:component-scan base-package="com.spring.mvc">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值