溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring?bean加載控制如何實(shí)現(xiàn)

發(fā)布時(shí)間:2022-12-28 09:41:32 來源:億速云 閱讀:104 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Spring bean加載控制如何實(shí)現(xiàn)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring bean加載控制如何實(shí)現(xiàn)”吧!

    1. Controller加載控制

    因?yàn)楣δ懿煌?,要避免Spring錯(cuò)誤的加載到SpringMVC的bean

    1.1 Controller加載控制與業(yè)務(wù)bean加載控制

    Spring?bean加載控制如何實(shí)現(xiàn)

    SpringMVC相關(guān)bean(表現(xiàn)層bean)

    Spring控制的bean

    • 業(yè)務(wù)bean(Service)

    • 功能bean(DataSource等)

    SpringMVC相關(guān)bean加載控制

    • SpringMVC加載的bean對應(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í)現(xiàn)

    證明加載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[]{"/"}; 
        }
    }

    簡化格式

    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};
        }
    }

    感謝各位的閱讀,以上就是“Spring bean加載控制如何實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring bean加載控制如何實(shí)現(xiàn)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI