相关疑难解决方法(0)

如何理解Spring @ComponentScan

我正在学习关于Spring MVC的教程,即使我阅读了spring API doc,我也无法理解@ComponentScan注释,所以这里是示例代码

配置视图控制器

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

基于注释的控制器

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");    
    }    
}     
Run Code Online (Sandbox Code Playgroud)

我的问题是:

对于View Controllers,通过添加
@Configuration和
@ComponentScan(basePackages = {"com.apress.prospringmvc.bookstore.web"})将在后台完成什么?com.apress.prospringmvc.bookstore.web包是否会为这个视图控制器提供一些东西?

spring-mvc

16
推荐指数
1
解决办法
3万
查看次数

标签 统计

spring-mvc ×1