springmvc的get請求跳轉不到controller的問題

我也是剛學spring不久,在剛照着別的項目配置完一個springmvc框架的時候,,發現在url寫請求,跳轉不到controller。經過百度和google後,發現我有個地方忽略了。。
就是<context:component-scan.../>的use-default-filters屬性,我設置爲false。因爲如果不設置這個值,默認爲true,就會掃描base-package屬性指定的包下的所有的類。

--這個在springmvc+spring+hibernate等集成時最容易出問題的地,最典型的錯誤就是:事務不起作用。

這我也是在百度才知道的。

-- <context:component-scan>會交給org.springframework.context.config.ContextNamespaceHandler處理;

registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());

-- ComponentScanBeanDefinitionParser會讀取配置文件信息並組裝成org.springframework.context.annotation.ClassPathBeanDefinitionScanner進行處理;
-- 如果沒有配置<context:component-scan>的use-default-filters屬性,則默認爲true,在創建ClassPathBeanDefinitionScanner時會根據use-default-filters是否爲true來調用如下代碼:

protected void registerDefaultFilters() { 
this.includeFilters.add(new AnnotationTypeFilter(Component.class)); 
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); 
try { 
    this.includeFilters.add(new AnnotationTypeFilter( 
            ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false)); 
    logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning"); 
} 
catch (ClassNotFoundException ex) { 
    // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip. 
} 
try { 
    this.includeFilters.add(new AnnotationTypeFilter( 
            ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false)); 
    logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning"); 
} 
catch (ClassNotFoundException ex) { 
    // JSR-330 API not available - simply skip. 
}

可以看到默認ClassPathBeanDefinitionScanner會自動註冊對@Component、@ManagedBean、@Named註解的Bean進行掃描。

我原本的配置只有Repositroty,和Service,少了對controller的掃描,所以纔會跳不到controller。
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章