<context:component-scan base-package="com.atguigu.atcrowdfunding.*" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
context:component-scan里面的use-default-filters默认值为true,使用默认的过滤器,会自动扫描带有@Component、@Repository、@Service和@Controller的类。
上面的配置表示扫描时会将Controller排除,而如果在use-default-filters默认值为true的时候配置context:include-filter
(注意是include,而如果是exclude-filter
那么则会报错,在idea会显示No matching beans found,因为默认过滤器关闭了,又没有指定include-filter,扫描不到bean)是没有意义的,因为
默认是扫描所有的类,所以一般上面的配置会在spring中配置,将Controller排除,交给springMVC容器管理。
<context:component-scan base-package="com.atguigu.atcrowdfunding.*" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
而这段配置则仅仅扫描Controller,通常在springMVc配置文件中配置,在"use-default-filters="false"的情况下,只扫描Controller,但是如果没有配置"use-default-filters="false",那么
事务是会加不上去的因为springMVC容器也会注入service类,根据就近原则,默认是会使用springMVC中的service,那么在spring中配置的事务就不起作用了。
(初学者,有误请指正,谢谢)