SpringMVC?bean實(shí)現(xiàn)加載控制方法詳解
1、Spring配置類排除加載SpringMVC的bean
SpringMVC 通常只需要加載 controller 包內(nèi)的 bean,而 Spring 需要加載 dao 和 service 包內(nèi)的 bean,為了省事,Spring 配置類經(jīng)常設(shè)置掃描的包為一個大范圍的包(包含 dao 和 service 在內(nèi)的包),此時 Spring 會錯誤或者多余地加載到 controller 包內(nèi)的 bean
使 Spring 排除加載 SpringMVC 的 bean 一般有兩種方法
- 方法 1:掃描包時精確掃描需要的包,而不用大范圍的掃描
- 方法 2:大范圍掃描,但是將不需要的 bean 過濾掉
方法 1 示例:
@Configuration
// 使 Spring 只掃描 dao 與 service 包
@ComponentScan({"com.mzz.dao", "com.mzz.service"})
public class SpringConfig {
}
方法 2:在 @ComponentScan 注解中可以設(shè)置 excludeFilters 屬性排除掉 不需要的 bean,excludeFilters 屬性的值是一個 Filter 注解,這個注解也在 ComponentScan 內(nèi),需要設(shè)置 type(過濾類型) 和 classes
下面的示例表示 Spring 的掃描范圍為 com.mzz 但是按注解類型過濾掉所有注解了 @Controller 的 bean
@Configuration
@ComponentScan(value = "com.mzz",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION, // 按照 注解 來進(jìn)行過濾
classes = {Controller.class} // 過濾所有注解了 @Controller 的 bean
)
)
public class SpringConfig {
}
由于 Filter 注解中 type 的默認(rèn)值就是 FilterType.ANNOTATION,所以上面的代碼可以簡寫成這樣:
@Configuration
@ComponentScan(value = "com.mzz",
excludeFilters = @ComponentScan.Filter({Controller.class})
)
public class SpringConfig {
}
[補(bǔ)充]
當(dāng) Spring 和 SpringMVC 的配置類同時存在時,上述的方法二可能不會生效,原因是 Spring 進(jìn)行大范圍掃描時可以掃描到 SpringMVC 的配置類,于是 SpringMVC 中的 bean 也被加載到 Spring 中
此時也有兩種解決方法
一是將 SpringMVC 的配置類移動到 Spring 掃描不到的包下(比如 com 包下)
二是 Spring 過濾時也將 SpringMVC 的配置類過濾掉,SpringMVC 的配置類會注解 @Configuration 和 @ComponentScan,可以對其進(jìn)行過濾
@Configuration
@ComponentScan(value = "com.mzz",
excludeFilters = @ComponentScan.Filter({Controller.class, ComponentScan.class})
)
public class SpringConfig {
}
2、Servlet容器配置類簡潔開發(fā)
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
// 加載 springMVC 容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
// 加載 spring 容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
}上面的類繼承自 AbstractDispatcherServletInitializer 抽象類,改為繼承 AbstractAnnotationConfigDispatcherServletInitializer 抽象類。則只需將配置類 class 返回即可,而不需要手動創(chuàng)建 Context,如下
public class ServletContainerInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
// 加載 springMVC 容器配置
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}視頻鏈接:傳送門
到此這篇關(guān)于SpringMVC bean實(shí)現(xiàn)加載控制方法詳解的文章就介紹到這了,更多相關(guān)SpringMVC bean加載控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis日志參數(shù)快速替換占位符工具的詳細(xì)步驟
這篇文章主要介紹了Mybatis日志參數(shù)快速替換占位符工具的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Spring Boot Jar 文件能直接運(yùn)行的操作方法
本文將深入探討 Spring Boot 的打包過程及其運(yùn)行原理,揭示其 JAR 文件如何巧妙地集成依賴項(xiàng)、嵌入 Web 容器并實(shí)現(xiàn)自動配置,感興趣的朋友跟隨小編一起看看吧2024-12-12
使用Sentinel自定義返回和實(shí)現(xiàn)區(qū)分來源方式
這篇文章主要介紹了使用Sentinel自定義返回和實(shí)現(xiàn)區(qū)分來源方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
基于SpringBoot后端導(dǎo)出Excel文件的操作方法
這篇文章給大家介紹了基于SpringBoot后端導(dǎo)出Excel文件的操作方法,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
通過Java計(jì)算文件的MD5值實(shí)現(xiàn)方式
本文將詳細(xì)介紹如何使用Java語言來計(jì)算文件的MD5值,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring Data JPA實(shí)現(xiàn)分頁P(yáng)ageable的實(shí)例代碼
本篇文章主要介紹了Spring Data JPA實(shí)現(xiàn)分頁P(yáng)ageable的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Java實(shí)現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能
這篇文章主要介紹了Java實(shí)現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能,涉及java針對Excel相關(guān)數(shù)值與字符串操作技巧,需要的朋友可以參考下2018-03-03

