spring和springMVC配置文件中的扫描包如何配置

原文链接:https://blog.csdn.net/weixin_43802688/article/details/90600611

 

我的项目大概文件路径:

 

 

然后进入主题:
spring的配置文件名称为applicationContext.xml
springMVC的配置文件名称为dispatcherServlet-servlet.xml

1.spring的配置文件中需要将Controller的注解排除掉。也就是排除@Controller。需要扫描到service和dao层的注解
可以用以下这种方式:

<context:component-scan base-package="com.dancer.crudr">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
 
2.springMVC的配置文件中需扫描Controller的注解。也就是扫描@Controller
可以用以下这种方式:

<context:component-scan base-package="com.dancer.crud" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
 
接下来具体分析:

//这个是格式,照抄就完了
<context:component-scan></context:component-scan>
 
//这个是要扫描的包,我是没填写完整路径
base-package
 
//这个默认是true,意思就是会自动对 @Component、@ManagedBeuse-default-filters="true"an、@Named注解的Bean进行扫描。
反之把他改为false则不对@Component、@ManagedBeuse-default-filters="true"an、@Named注解的Bean进行扫描
use-default-filters
 
//这个是排除的意思
exclude-filter
 
//这个是包含的意思
include-filter
 
spring分析:

//自动扫描com.dancer.crudr包下的所有注解
<context:component-scan base-package="com.dancer.crudr">
//但是排除Controller的注解
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
//exclude-filter:排除,得到除Controller以外的注解
</context:component-scan>
 

 


springMVC分析:

//不会自动扫描com.dancer.crudr包下的所有注解,因为use-default-filters改为false
<context:component-scan base-package="com.dancer.crud" use-default-filters="false">
//获取Controller的注解
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
//use-default-filters:不会自自动扫描
//include-filter:包含,得到Controller的注解
</context:component-scan>
————————————————
版权声明:本文为CSDN博主「茂念」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

posted @ 2021-11-03 15:26  kelelipeng  阅读(2267)  评论(0编辑  收藏  举报