@Controller注解表示一个特定的类作为控制器。Spring不要求你扩展任何控制器基类或者引用ServletAPI,然而如果有必要,你仍然需要引用特定的ServletAPI。
@Controller作为注解类的一个原型,表示了它的角色。 dispatcher 会扫描有注解的类的映射方法,
并且检测@RequestMapping注解。
你可以在 dispatcher’s context中使用一个标准的Spring bean定义很明确的定义一个注解控制bean。
然而@Controller 也支持自动检测,与Spring一样一般支持检测classpath下的组件类和自动注册的bean定义它们。
为了能自动检测这样的注解类,你需要添加一个component scanning (扫描组件)在你的配置里,使用
Spring的 spring-context模式,例如下面的例子。
<?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>