<context:component-scan base-package="com.springmvc" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
以上,自动扫描com.springmvc下的@Component @Controller @Service等这些注解的类,把这些类注册为bean。
由于扫描力度过大,对不需要的注解类不进行扫描,所以引入context:include-filter和context:exclude-filter。
<context:component-scan base-package="com.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
以上,这里不需要扫描@Service注解类,所以引入exclude-filter,对不需要扫描的@Service注解类不进行扫描。
<context:component-scan base-package="com.springmvc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
以上,这里只需要扫描@Controller注解类,所以引入include-filter,只对@Controller注解类进行扫描。
<context:component-scan base-package="cn.springmvc"/>
不过最常用的还是这一种—开启注解扫描,自动扫描包,让指定包下的注解生效,由IOC容器统一管理。