springmvc配置说明:context:component-scan
<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注解类进行扫描。
本文介绍了SpringMVC中如何使用context:component-scan进行组件扫描,并通过include-filter和exclude-filter来精确控制哪些注解的类会被扫描并注册为bean。
648

被折叠的 条评论
为什么被折叠?



