views:

111

answers:

2

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 />
  1. Do you really need both?

  2. 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.

+1  A: 
  1. mvc:annotation-driven allows you to configure behavior of Spring MVC. See details in documentation .For basic usage of Spring MVC you do not need it.
  2. If you need more than one package just mention parent one: <context:component-scan base-package="org.springframework.samples.mvc" />
uthark
so having mvc:annotation-driven is doing nothing?
Blankman
No, `mvc:annotation-driven` allows you to configure behavior of Spring MVC. See details in documentation http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/mvc.html#mvc-annotation-drivenFor basic usage of Spring MVC you do not need it.
uthark
+5  A: 

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"/>
Arthur Ronald F D Garcia
Just for curiosity: context:component-scan includes AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor which means means that the two components are autodetected and wired together - **all without any bean configuration metadata provided in XML**
Arthur Ronald F D Garcia
Thanks, I didn't want to do br.com.app since it will be scanning potentially more classes than required. great answer! btw what's the diff between a @component and @service ?
Blankman
@Blankman None. @Service is also a @Component annotation. See here: http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/stereotype/Service.html It is just a special metadata to **denote a service component in the service layer**.
Arthur Ronald F D Garcia