春天基本的MVC示例应用程序,标注混乱扫描 [英] spring basic mvc sample application, annotation scan confusion
问题描述
小糊涂,基本的Spring MVC应用程序有这样的:
APP-config.xml中
<背景:组件扫描基包=org.springframework.samples.mvc.basic/>
和MVC-config.xml中有:
<! - 配置了@Controller编程模型 - >
< MVC:注解驱动/>
-
你真的需要这两者?
-
有关组件扫描,这是否意味着,如果我不把我@Controller和@Service标志将没有任何效果了正确的包路径?
如果我需要一个以上的包,我只是重复的条目?
我试过只使用MVC:注解驱动,但没有工作,我不得不把com.example.web.controllers在组件扫描XML节点,使其工作
的背景:组件扫描的清晰
中扫描将自动注册为Spring bean的注释组件的类路径。 默认,由Spring提供的@Component,@Repository,@Service和@Controller定型被检测到。
块引用>所以@Controller只是一个Spring bean。没有别的。
和
的 MVC:注解驱动的
注册HandlerMapping的和需要的HandlerAdapter 以请求分派给您@Controllers
块引用>这是类似
< bean类=org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping/>
< bean类=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter/>如果我需要一个以上的包,我只是重复的条目?
您可以的,如果你想要的。的背景:组件扫描的只是一个bean后处理器
<背景:组件扫描基包=br.com.app.view.controller/>
<背景:组件扫描基包=br.com.app.service/>或者
使用逗号分隔的包的列表,以扫描注释部分。
块引用><背景:组件扫描基包=br.com.app.view.controller,br.com.app.service/>
Little confused, the basic spring mvc app has this:
app-config.xml
<context:component-scan base-package="org.springframework.samples.mvc.basic" />
and the mvc-config.xml has:
<!-- Configures the @Controller programming model --> <mvc:annotation-driven />
Do you really need both?
for component-scan, does this mean if I don't put the correct package path my @Controller and @Service markers will have no effect? If I need more than one package, do I just duplicate the entry?
I tried using just the mvc:annotation-driven but that didn't work, I had to put com.example.web.controllers in the component-scan xml node to make it work.
解决方案context:component-scan is clear
Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected.
So @Controller is just a Spring bean. Nothing else.
And
mvc:annotation-driven
registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers
Which is similar to
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
If I need more than one package, do I just duplicate the entry?
You can if you want. context:component-scan is just a bean post-processor.
<context:component-scan base-package="br.com.app.view.controller"/> <context:component-scan base-package="br.com.app.service"/>
Or
Use a comma-separated list of packages to scan for annotated components.
<context:component-scan base-package="br.com.app.view.controller,br.com.app.service"/>
这篇关于春天基本的MVC示例应用程序,标注混乱扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!