国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot復(fù)習(xí):(20)如何把bean手動注冊到容器?

這篇具有很好參考價值的文章主要介紹了SpringBoot復(fù)習(xí):(20)如何把bean手動注冊到容器?。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

可以通過實現(xiàn)BeanDefinitionRegistryPostProcessor接口,它的父接口是BeanFactoryPostProcessor.
步驟:
一、自定義一個組件類:

package com.example.demo.service;


public class MusicService {
    public MusicService() {
        System.out.println("music service constructed!");
    }
}

二、定義類實現(xiàn)BeanDefinitionRegistryPostProcessor:

package com.example.demo.component;

import com.example.demo.service.MusicService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println(registry.getBeanDefinitionCount());
        //定義BeanDefinition對象
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MusicService.class);
        //將BeanDefinition對象注冊到容器
        registry.registerBeanDefinition("musicBean", rootBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println( beanFactory.getBeanDefinitionCount() );
    }
}

通過@Component注解,Spring就能夠掃描到MyBeanDefinitionRegistryPostProcessor,也就能夠把MusicService這個組件注冊到容器。
三、可以獲取在容器中通過MyBeanDefinitionRegistryPostProcessor注冊的bean文章來源地址http://www.zghlxwxcb.cn/news/detail-628688.html

package com.example.demo;

import com.example.demo.service.MusicService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource("classpath:my.properties")
public class DemoApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
		System.out.println("abc");

		MusicService musicService = context.getBean("musicBean", MusicService.class);
		System.out.println(musicService);

	}

}

到了這里,關(guān)于SpringBoot復(fù)習(xí):(20)如何把bean手動注冊到容器?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring 容器原始 Bean 是如何創(chuàng)建的?

    Spring 容器原始 Bean 是如何創(chuàng)建的?

    以下內(nèi)容基于 Spring6.0.4。 這個話題其實非常龐大,我本來想從 getBean 方法講起,但一想這樣講完估計很多小伙伴就懵了,所以我們還是一步一步來,今天我主要是想和小伙伴們講講 Spring 容器創(chuàng)建 Bean 最最核心的 createBeanInstance 方法,這個方法專門用來創(chuàng)建一個原始 Bean 實例

    2024年02月14日
    瀏覽(20)
  • spring注冊bean和注入bean的方法

    一個對象加入到Spring容器中。 XML配置方式 @Component注解@Controler、@Service、@Repository + @ComponentScan包掃描方式 @Configuration + @Bean方式 @Import方式 @Import + ImportSelector方式 @Import + ImportBeanDefinitionRegistrar方式 FactoryBean方式 BeanDefinitionRegistryPostProcessor方式 BeanFactoryPostProcessor方式 參考:添

    2024年02月15日
    瀏覽(18)
  • spring復(fù)習(xí):(18)給bean的屬性賦值

    spring復(fù)習(xí):(18)給bean的屬性賦值

    類: AbstractAutowireCapableBeanFactory: 其中populateBean用來用我們配置文件里的屬性來給bean的屬性賦值: 其中applyPropertyValues(beanName, mbd, bw, pvs);真正進行了賦值. 其中調(diào)用了bw.setPropertyValues:,代碼: setPropertyValues代碼: 其中調(diào)用了:setPropertyValue(pv);這個方法的代碼: 調(diào)用setPropertyVal

    2024年02月16日
    瀏覽(31)
  • SpringBoot復(fù)習(xí):(45)@Component定義的bean會被@Bean定義的同名的bean覆蓋

    SpringBoot復(fù)習(xí):(45)@Component定義的bean會被@Bean定義的同名的bean覆蓋

    有同名的bean需要配置: spring.main.allow-bean-definition-overriding=true 否則報錯。 運行結(jié)果

    2024年02月13日
    瀏覽(14)
  • SpringBoot 底層機制分析【Tomcat 啟動+Spring 容器初始化+Tomcat 如何關(guān)聯(lián)Spring 容器】【下】

    SpringBoot 底層機制分析【Tomcat 啟動+Spring 容器初始化+Tomcat 如何關(guān)聯(lián)Spring 容器】【下】

    ??前言 本篇博文是關(guān)于SpringBoot 底層機制分析實現(xiàn),希望能夠幫助你更好的了解SpringBoot ?? ??個人主頁:晨犀主頁 ??個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力???? ??歡迎大家:這里是CSDN,我總結(jié)知識的地方,歡迎來到我的博客

    2024年02月13日
    瀏覽(28)
  • spring注解驅(qū)動開發(fā)(BEAN注冊方式與生命周期)

    spring注解驅(qū)動開發(fā)(BEAN注冊方式與生命周期)

    目錄 容器中注冊BEAN的方式 BEAN生命周期 包掃描+組件標注注解 @ComponentScan(basePackages = {\\\"com.an.spring.condition\\\"}) @Service @Component @Controller @Repository @BEan方式【導(dǎo)入第三方包里面的組件】 @Import快速給容器中導(dǎo)入一個組件。 1)、@IMport(要導(dǎo)入到容器中的組件),容器就會注入這個組

    2024年02月07日
    瀏覽(48)
  • 關(guān)于Spring Bean容器的理解

    Spring Bean 對象 Spring Bean 是Spring框架中的一個核心概念。在Spring框架中,Bean是指那些組成應(yīng)用程序的對象,它們由Spring IoC容器實例化、組裝和管理。Bean是一個被實例化、組裝和由Spring IoC容器所管理的對象。 Spring Bean 容器 Spring Bean容器是Spring框架的核心部分,負責管理應(yīng)用程

    2024年01月16日
    瀏覽(43)
  • spring復(fù)習(xí):(35)在getBean時,在哪里根據(jù)普通bean和工廠bean進行區(qū)分處理來返回的?

    spring復(fù)習(xí):(35)在getBean時,在哪里根據(jù)普通bean和工廠bean進行區(qū)分處理來返回的?

    在AbstractBeanFactory的doGetBean方法: 調(diào)用的getObjectForBeanInstance方法部分代碼如下: 如果不是工廠bean,則直接將實例返回,否則調(diào)用getObjectFromFactoryBean方法獲取工廠bean的getObject方法返回的對象 其中調(diào)用了doGetObjectFromFactoryBean方法,代碼如下: 可以看到工廠bean的getObject方法被調(diào)用

    2024年02月16日
    瀏覽(29)
  • spring高級源碼50講-1-8(spring容器與bean)

    1) 容器接口 BeanFactory 接口,典型功能有: getBean ApplicationContext 接口,是 BeanFactory 的子接口。它擴展了 BeanFactory 接口的功能,如: 國際化 通配符方式獲取一組 Resource 資源 整合 Environment 環(huán)境(能通過它獲取各種來源的配置信息) 事件發(fā)布與監(jiān)聽,實現(xiàn)組件之間的解耦 可以

    2024年02月10日
    瀏覽(14)
  • Spring——Bean注入幾種方式(放入容器)

    Spring——Bean注入幾種方式(放入容器)

    個人博客: 全是干貨,相信不會讓你失望 1.XML方式注入 在現(xiàn)在這個Springboot橫行的年代,以XML來注入的方式可能已經(jīng)不多見了,因為壓根用不著,但畢竟是注入方式之一也得提一提,這種方式就是依賴于XML的解析來獲取我們需要注入的Bean對象 常見的方式有:set方法注入、構(gòu)

    2024年02月01日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包