Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand Spring @ComponentScan

Tags:

I'm following a tutorial about Spring MVC and I cannot understand something about the @ComponentScan annotation even after reading the spring API doc, so here is the sample code:

Configuring View Controllers

package com.apress.prospringmvc.bookstore.web.config;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
// Other imports ommitted
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
    // Other methods ommitted
    @Override
    public void addViewControllers(final ViewControllerRegistry registry) {
        registry.addViewController("/index.htm").setViewName("index");
    }
}

Annotation-based Controllers

package com.apress.prospringmvc.bookstore.web;    
import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.servlet.ModelAndView;    
@Controller    
public class IndexController {    
@RequestMapping(value = "/index.htm")    
    public ModelAndView indexPage() {     
        return new ModelAndView("index");    
    }    
}     

My question is, for View Controllers, by adding @Configuration and @ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" }), what will be done in the background? Will the package com.apress.prospringmvc.bookstore.web offer something for these view controllers?

like image 696
android_ming Avatar asked Mar 10 '15 12:03

android_ming


People also ask

What does @ComponentScan do in Spring?

Using @ComponentScan in a Spring Application. With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What does @ComponentScan mean?

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

Which of the following is best practices of using @ComponentScan annotation in Spring?

A good practice is to explicitly import a @Configuration class with the @Import annotation and add the @ComponentScan annotation to that configuration class to auto-scan only the package of that class.

What is difference between @component and @ComponentScan?

How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.


1 Answers

Simply put - @ComponentScan tells Spring in which packages you have annotated classes which should be managed by Spring. So, for example, if you have a class annotated with @Controller which is in a package which is not scanned by Spring, you will not be able to use it as Spring controller.

Classes annotated with @Configuration is a new way of configuring Spring using annotations instead of XML files (it's called Java configuration). Spring needs to know which packages contain spring beans, otherwise you would have to register each bean individually. That's what @ComponentScan is used for.

In your example, you tell Spring that package com.apress.prospringmvc.bookstore.web contains classes which should be handled by Spring. Spring then finds a class annotated with @Controller, and processes it, which results in all requests coming to /index.htm being intercepted by the controller.

When the request is intercepted, Spring needs to know what response to send to the caller. Since you return an instance of ModelAndView, it will try to find a view (JSP page) called index in your project (details of this depend on configured view resolvers), and renders it to the user.

If @Controller annotation wasn't present, or that package wasn't scanned by Spring, all of this would not be possible.

like image 99
Predrag Maric Avatar answered Sep 20 '22 13:09

Predrag Maric