Spring bean加載控制實(shí)現(xiàn)方法
1. Controller加載控制
因?yàn)楣δ懿煌苊釹pring錯(cuò)誤的加載到SpringMVC的bean
1.1 Controller加載控制與業(yè)務(wù)bean加載控制

SpringMVC相關(guān)bean(表現(xiàn)層bean)
Spring控制的bean
- 業(yè)務(wù)bean(Service)
- 功能bean(DataSource等)
SpringMVC相關(guān)bean加載控制
- SpringMVC加載的bean對(duì)應(yīng)的包均在com.zhang.controller包內(nèi)
Spring相關(guān)bean加載控制
- 方式一:Spring加載的bean設(shè)定掃描范圍為com.zhang,排除掉controller包內(nèi)的bean
- 方式二:Spring加載的bean設(shè)定掃描范圍為精準(zhǔn)范圍,例如service包、dao包等
- 方式三:不區(qū)分Spring與SpringMVC的環(huán)境,加載到同一個(gè)環(huán)境中
1.2 加載Spring控制的bean的時(shí)候排除掉SpringMVC控制的bean(方式一)
名稱:@ComponentScan
類型:類注解
范例
package com.zhang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.zhang", excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
))
public class SpringConfig {
}屬性
- excludeFilters:排除掃描路徑中加載的bean,需要指定類別(type)與具體項(xiàng)(classes)
- includeFilters:加載指定的bean,需要指定類別(type)與具體項(xiàng)(classes)
1.3 驗(yàn)證是否排除成功
創(chuàng)建spring容器加載spring配置文件,然后根據(jù)類型獲取表現(xiàn)層的bean,如果不能獲取則證明加載Spring控制的bean的時(shí)候成功排除掉SpringMVC控制的bean;這里值得一提的是,需要把SpringMVCConfig配置類上的@Configuration注釋掉;
package com.zhang;
import com.zhang.config.SpringConfig;
import com.zhang.controller.UserController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserController userController = context.getBean(UserController.class);
System.out.println(userController);
}
}1.4 運(yùn)行結(jié)果

證明加載Spring控制的bean的時(shí)候成功排除掉SpringMVC控制的bean
2. Bean的加載格式
extends AbstractDispatcherServletInitializer
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
簡(jiǎn)化格式
extends AbstractAnnotationConfigDispatcherServletInitializer
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class}
};
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
}
到此這篇關(guān)于Spring bean加載控制實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Spring bean加載控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot框架DataSource多數(shù)據(jù)源配置方式
這篇文章主要介紹了SpringBoot框架DataSource多數(shù)據(jù)源配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringBoot前后端接口對(duì)接常見(jiàn)錯(cuò)誤小結(jié)
SpringBoot前后端接口對(duì)接工作時(shí),經(jīng)常遇到請(qǐng)求500,400等問(wèn)題,本文主要介紹了SpringBoot前后端接口對(duì)接常見(jiàn)錯(cuò)誤小結(jié),感興趣的可以了解一下2022-01-01
Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題
這篇文章主要介紹了Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問(wèn)題及解決
這篇文章主要介紹了java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
String字符串拼接方法concat和+的效率對(duì)比
這篇文章主要介紹了String字符串拼接方法concat和+的效率對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java 數(shù)據(jù)庫(kù)連接池詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Java 數(shù)據(jù)庫(kù)連接池詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12

