在 Spring 框架中,每個(gè) bean 必須至少有一個(gè)唯一的名稱(chēng)。Spring 遵循簡(jiǎn)單且默認(rèn)的命名策略來(lái)確定 bean 的名稱(chēng),無(wú)論我們使用 XML 配置
還是基于Java代碼配置。本文將詳細(xì)討論這些策略。
默認(rèn)情況下,Spring會(huì)使用聲明Bean類(lèi)型的簡(jiǎn)單名稱(chēng),將第一個(gè)字母改為小寫(xiě),并使用生成的值來(lái)命名Bean。此種方式適用于所有定型注解(@Service、@Repository 等)。
下面我我們聲明一個(gè)非常簡(jiǎn)單的bean,如下所示:
@Configuration@ComponentScanpublic class AppConfig { //...}@Componentpublic class DemoBean { //...}
DemoBean使用@Component注解,當(dāng)我們從應(yīng)用程序上下文中檢索 bean 并打印其名稱(chēng)時(shí),它會(huì)打印“ demoBean ”。
var applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
程序輸出:
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)建的beanappConfig和demoBean.
當(dāng)我們使用@Bean注解來(lái)創(chuàng)建一個(gè)新的bean時(shí),該bean將以創(chuàng)建它的方法命名。
讓我們通過(guò)一個(gè)示例來(lái)理解,我們創(chuàng)建兩個(gè)具有不同方法名稱(chēng)的DemoBean類(lèi)型的 bean 。
@Configurationpublic class AppConfig { @Bean DemoBean demoBean(){ return new DemoBean(); } @Bean DemoBean anotherDemoBean(){ return new DemoBean(); }}
當(dāng)我們運(yùn)行代碼并打印bean名稱(chēng)時(shí),會(huì)輸出以下結(jié)果:
...appConfigdemoBeananotherDemoBean
對(duì)于所有的注解類(lèi)型,都有一個(gè)默認(rèn)屬性名為"value",可以用一個(gè)值進(jìn)行初始化,作為用于標(biāo)識(shí)bean的名稱(chēng)。
@Component(value = "newBeanName")public class DemoBean { //...}
注意,@Component(value = "newBeanName") 等同于 @Component("newBeanName")。它們產(chǎn)生一樣的結(jié)果。
同樣@Bean注解有兩個(gè)屬性name 和 value,可以為bean定義一個(gè)顯式名稱(chēng)。
@Configurationpublic class AppConfig { @Bean(name = "newBeanName") DemoBean demoBean(){ return new DemoBean(); } @Bean(value = "anotherNewBeanName") DemoBean anotherDemoBean(){ return new DemoBean(); }}
當(dāng)我們運(yùn)行代碼并打印bean名稱(chēng)時(shí),會(huì)輸出以下結(jié)果:
...appConfignewBeanNameanotherNewBeanName
@Bean 注解的 name 或 value 屬性可以指定一個(gè)值數(shù)組,用于引用 bean 的名稱(chēng)。當(dāng)這樣做時(shí),數(shù)組中的第一個(gè)值將成為主要名稱(chēng),而其他值將成為別名。
@Bean(value = {"newBeanName", "newBeanName-1", "newBeanName-2"})DemoBean demoBean(){ return new DemoBean();}
現(xiàn)在,當(dāng)打印 bean 的名稱(chēng)時(shí),它仍然是 "newBeanName"。但是當(dāng)我們打印 bean 的名稱(chēng)別名時(shí),我們會(huì)得到額外的名稱(chēng),即 "newBeanName-1" 和 "newBeanName-2"。
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); }});
輸出
...appConfignewBeanNamenewBeanName-2newBeanName-1
與Spring中的所有功能類(lèi)似,bean的命名也可以進(jìn)行自定義。為了進(jìn)行自定義名稱(chēng)生成,我們可以定義一個(gè)類(lèi),繼承 AnnotationBeanNameGenerator 并在 @ComponentScan 注解中指定該類(lèi)的名稱(chēng)。
@Configuration@ComponentScan(nameGenerator = CustomBeanNameGenerator.class)public class AppConfig { //...}
接下來(lái),我們通過(guò)在 CustomBeanNameGenerator 類(lèi)中重寫(xiě) buildDefaultBeanName() 方法來(lái)定義自定義的名稱(chēng)生成邏輯。
以下示例會(huì)返回由小寫(xiě)的簡(jiǎn)單類(lèi)名與唯一標(biāo)識(shí)符連接而成的 bean 名稱(chēng)。
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; }}
輸出
appConfigdemobean-889ed00b
在本Spring教程中,我們學(xué)習(xí)了5種bean命名策略希望對(duì)你有所幫助。
本文鏈接:http://www.www897cc.com/showinfo-26-37241-0.htmlSpring Bean 命名各種方式,看這一篇就夠了
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com