定义Spring的包扫描规则
适用场景:
对于下面这样的建包方式,如果在spring的xml中想要自定义扫描所有的controller又不想扫描到service包下面的Bean


具体做法:是根据注解的类型进行分类区分
在父容器的配置文件中排除controller
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
在子容器中包含controller
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
本文介绍如何在Spring中自定义包扫描规则,通过使用注解过滤器实现只扫描特定类型的组件,如Controller,同时避免扫描Service等其他类型的Bean,提高应用程序的加载效率。
952

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



