1. 程式人生 > 其它 >spring和springMVC配置檔案中的掃描包如何配置

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版權協議,轉載請附上原文出處連結及本宣告。