天天看点

bean扫描的配置详解1.mvc:annotation-driven2.context:annotation-config3.context:component-scan

bean扫描的配置详解1.mvc:annotation-driven2.context:annotation-config3.context:component-scan

1.mvc:annotation-driven

:确定调用哪个controller的哪个方法来处理当前请求。如果没有回找不到@RequestMapping指定的路径。

2.context:annotation-config

扫描的注解如下:@Autowired,@Resource 、@PostConstruct、@PreDestroy,@PersistenceContext,@Required。

3.context:component-scan

:context:component-scan做了context:annotation-config要做的事情,还额外支持@Component,@Repository,@Service, @Controller @RestController, @ControllerAdvice, and @Configuration 注解。

所以配置context:component-scan就不需要配置context:annotation- config。

<context:component-scan base-package="test.*.controller" use-default-filters="false">        
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>     
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 
     </context:component-scan>      
use-default-filters=“false”,不使用默认过滤器(所有包都不自动扫描),需要

,全部包都扫描,除了标签里面配置的。

<context:component-scan base-package="test.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
      </context:component-scan>       

继续阅读