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版權協議,轉載請附上原文出處鏈接及本聲明。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM