AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

Spring Bean 命名各種方式,看這一篇就夠了

來源: 責(zé)編: 時(shí)間:2023-12-04 09:19:40 303觀看
導(dǎo)讀在 Spring 框架中,每個(gè) bean 必須至少有一個(gè)唯一的名稱。Spring 遵循簡單且默認(rèn)的命名策略來確定 bean 的名稱,無論我們使用 XML 配置還是基于Java代碼配置。本文將詳細(xì)討論這些策略。1.使用@Component的默認(rèn)Bean命名默

在 Spring 框架中,每個(gè) bean 必須至少有一個(gè)唯一的名稱。Spring 遵循簡單且默認(rèn)的命名策略來確定 bean 的名稱,無論我們使用 XML 配置iN128資訊網(wǎng)——每日最新資訊28at.com

還是基于Java代碼配置。本文將詳細(xì)討論這些策略。iN128資訊網(wǎng)——每日最新資訊28at.com

1.使用@Component的默認(rèn)Bean命名

默認(rèn)情況下,Spring會使用聲明Bean類型的簡單名稱,將第一個(gè)字母改為小寫,并使用生成的值來命名Bean。此種方式適用于所有定型注解(@Service@Repository 等)。iN128資訊網(wǎng)——每日最新資訊28at.com

下面我我們聲明一個(gè)非常簡單的bean,如下所示:iN128資訊網(wǎng)——每日最新資訊28at.com

@Configuration@ComponentScanpublic class AppConfig { //...}@Componentpublic class DemoBean {  //...}

DemoBean使用@Component注解,當(dāng)我們從應(yīng)用程序上下文中檢索 bean 并打印其名稱時(shí),它會打印“ demoBean ”。iN128資訊網(wǎng)——每日最新資訊28at.com

var applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);

程序輸出:iN128資訊網(wǎng)——每日最新資訊28at.com

org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.annotation.internalPersistenceAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactoryappConfigdemoBean

在以上結(jié)果輸出中,我們可以看到 Spring 創(chuàng)建的基礎(chǔ)設(shè)施 bean,還有我們創(chuàng)建的beanappConfigdemoBean.iN128資訊網(wǎng)——每日最新資訊28at.com

2. 使用@Bean的默認(rèn)Bean命名

當(dāng)我們使用@Bean注解來創(chuàng)建一個(gè)新的bean時(shí),該bean將以創(chuàng)建它的方法命名。iN128資訊網(wǎng)——每日最新資訊28at.com

讓我們通過一個(gè)示例來理解,我們創(chuàng)建兩個(gè)具有不同方法名稱的DemoBean類型的 bean 。iN128資訊網(wǎng)——每日最新資訊28at.com

@Configurationpublic class AppConfig {  @Bean  DemoBean demoBean(){    return new DemoBean();  }  @Bean  DemoBean anotherDemoBean(){    return new DemoBean();  }}

當(dāng)我們運(yùn)行代碼并打印bean名稱時(shí),會輸出以下結(jié)果:iN128資訊網(wǎng)——每日最新資訊28at.com

...appConfigdemoBeananotherDemoBean

3. 帶有值的顯式 Bean 命名

對于所有的注解類型,都有一個(gè)默認(rèn)屬性名為"value",可以用一個(gè)值進(jìn)行初始化,作為用于標(biāo)識bean的名稱。iN128資訊網(wǎng)——每日最新資訊28at.com

@Component(value = "newBeanName")public class DemoBean { //...}

注意,@Component(value = "newBeanName") 等同于 @Component("newBeanName")。它們產(chǎn)生一樣的結(jié)果。iN128資訊網(wǎng)——每日最新資訊28at.com

同樣@Bean注解有兩個(gè)屬性name  value,可以為bean定義一個(gè)顯式名稱。iN128資訊網(wǎng)——每日最新資訊28at.com

@Configurationpublic class AppConfig {  @Bean(name = "newBeanName")  DemoBean demoBean(){    return new DemoBean();  }  @Bean(value = "anotherNewBeanName")  DemoBean anotherDemoBean(){    return new DemoBean();  }}

當(dāng)我們運(yùn)行代碼并打印bean名稱時(shí),會輸出以下結(jié)果:iN128資訊網(wǎng)——每日最新資訊28at.com

...appConfignewBeanNameanotherNewBeanName

4. Bean 名稱別名:多個(gè) Bean 名稱

@Bean 注解的 name 或 value 屬性可以指定一個(gè)值數(shù)組,用于引用 bean 的名稱。當(dāng)這樣做時(shí),數(shù)組中的第一個(gè)值將成為主要名稱,而其他值將成為別名。iN128資訊網(wǎng)——每日最新資訊28at.com

@Bean(value = {"newBeanName", "newBeanName-1", "newBeanName-2"})DemoBean demoBean(){  return new DemoBean();}

現(xiàn)在,當(dāng)打印 bean 的名稱時(shí),它仍然是 "newBeanName"。但是當(dāng)我們打印 bean 的名稱別名時(shí),我們會得到額外的名稱,即 "newBeanName-1" 和 "newBeanName-2"。iN128資訊網(wǎng)——每日最新資訊28at.com

var applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);var demoBean = applicationContext.getBeansOfType(DemoBean.class);demoBean.forEach((k, v) -> {  var aliases = applicationContext.getAliases(k);  if (aliases.length > 0) {    Arrays.stream(aliases).forEach(System.out::println);  }});

輸出iN128資訊網(wǎng)——每日最新資訊28at.com

...appConfignewBeanNamenewBeanName-2newBeanName-1

5.生成自定義Bean名稱

與Spring中的所有功能類似,bean的命名也可以進(jìn)行自定義。為了進(jìn)行自定義名稱生成,我們可以定義一個(gè)類,繼承 AnnotationBeanNameGenerator 并在 @ComponentScan 注解中指定該類的名稱。iN128資訊網(wǎng)——每日最新資訊28at.com

@Configuration@ComponentScan(nameGenerator = CustomBeanNameGenerator.class)public class AppConfig { //...}

接下來,我們通過在 CustomBeanNameGenerator 類中重寫 buildDefaultBeanName() 方法來定義自定義的名稱生成邏輯。iN128資訊網(wǎng)——每日最新資訊28at.com

以下示例會返回由小寫的簡單類名與唯一標(biāo)識符連接而成的 bean 名稱。iN128資訊網(wǎng)——每日最新資訊28at.com

public class CustomBeanNameGenerator extends AnnotationBeanNameGenerator {  @Override  protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {    var beanName = definition.getBeanClassName()      .substring(definition.getBeanClassName().lastIndexOf(".") + 1)      .toLowerCase(Locale.ROOT);    var uid = UUID.randomUUID().toString().replace("-","").substring(0,8);    return beanName + "-" + uid;  }}

輸出iN128資訊網(wǎng)——每日最新資訊28at.com

appConfigdemobean-889ed00b

總結(jié)

在本Spring教程中,我們學(xué)習(xí)了5種bean命名策略希望對你有所幫助。iN128資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-37241-0.htmlSpring Bean 命名各種方式,看這一篇就夠了

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 《輻射》真人劇集新海報(bào):女主角路西踏出避難所

下一篇: 使用Ruff改善Python編程風(fēng)格

標(biāo)簽:
  • 熱門焦點(diǎn)
Top