Spring Bean名稱
- Spring
- java
1.概述
當我們有多個相同類型的實現時,命名Spring bean會很有幫助。這是因為,如果我們的bean沒有唯一的名稱,Spring會注入一個bean。
通過控制bean的命名,我們可以告訴Spring我們要將哪個bean注入目標對象。
在本文中,我們將討論Spring bean的命名策略,並探討如何為單一類型的bean提供多個名稱。
2.默認Bean命名策略
Spring提供了多個用於創建bean的註解。我們可以在不同級別使用這些註解。例如,我們可以在bean類上放置一些註解,而在創建bean的方法上放置其他註解。
首先,讓我們看看Spring的默認命名策略。當我們只指定沒有任何值的註釋時,Spring如何命名我們的bean?
2.1 類級註解
讓我們從類級別使用的註釋的默認命名策略開始。為了命名bean, Spring使用類名並將第一個字母轉換為小寫。
讓我們看一個例子:
@Service
public class LoggingService {
}
在這裡,Spring為類LoggingService
創建了一個bean,並使用名稱“ loggingService
”註冊了它。
相同的默認命名策略適用於用於創建Spring bean的所有類級別的註釋,例如@Component
, @Service
@Controller
。
2.2 方法級註解
Spring提供了@Bean
和@Qualifier
類的註解,可用於創建bean的方法。
讓我們看一個示例,以了解@Bean
註解的默認命名策略:
@Configuration
public class AuditConfiguration {
@Bean
public AuditService audit() {
return new AuditService();
}
}
在此配置類中,Spring在名稱“ audit
” AuditService
的bean,因為當我們在方法上@Bean
**批註時,Spring會將方法名稱用作bean名稱**。
我們還可以@Qualifier
批註,下面將提供一個示例。
3. Bean的自定義命名
當我們需要在相同的Spring上下文中創建多個相同類型的bean時,可以為這些bean提供自定義名稱,並使用這些名稱引用它們。
因此,讓我們看看如何為Spring bean賦予自定義名稱:
@Component("myBean")
public class MyCustomComponent {
}
這次,Spring將創建名稱為“ myBean
”的MyCustomComponent
當我們為bean明確命名時,Spring將使用該名稱,然後可以使用該名稱來引用或訪問bean。
類似於@Component(“myBean”)
,我們可以使用其他註釋來指定名稱,例如@Service(“myService”)
, @Controller(“myController”)
和@Bean(“myCustomBean”)
,然後Spring將註冊具有給定名稱的那個bean。
4.用@Bean
和@Qualifier
4.1 @Bean
與價值
正如我們前面所看到的, @Bean
註解是在方法級別應用的,默認情況下,Spring使用方法名稱作為Bean名稱。
可以覆蓋默認的Bean名稱-我們可以使用@Bean
批註指定值:
@Configuration
public class MyConfiguration {
@Bean("beanComponent")
public MyCustomComponent myComponent() {
return new MyCustomComponent();
}
}
在這種情況下,當我們要獲取類型為MyCustomComponent
的bean時,可以使用名稱“ beanComponent
”來引用該bean。
Spring @Bean
批註通常在配置類方法中聲明。它可以通過直接調用它們@Bean
4.2 @Qualifier
有價值
我們還可以使用@Qualifier
批註來命名bean。
首先,讓我們創建一個接口Animal
,該接口將由多個類實現:
public interface Animal {
String name();
}
現在,讓我們定義一個實現類Cat
並向其添加@Qualifier
批註,其值為“ cat
”:
@Component
@Qualifier("cat")
public class Cat implements Animal {
@Override
public String name() {
return "Cat";
}
}
Animal
的另一種實現, @Qualifier
和值“ dog
”對其進行註釋:
@Component
@Qualifier("dog")
public class Dog implements Animal {
@Override
public String name() {
return "Dog";
}
}
現在,讓我們編寫一個PetShow
類,在其中可以注入Animal
的兩個不同實例:
@Service
public class PetShow {
private final Animal dog;
private final Animal cat;
public PetShow (@Qualifier("dog")Animal dog, @Qualifier("cat")Animal cat) {
this.dog = dog;
this.cat = cat;
}
public Animal getDog() {
return dog;
}
public Animal getCat() {
return cat;
}
}
Pet
Show,
類中,我們通過@Qualifier
Animal
類型的兩種實現,並在每個批註的value屬性中使用了合格的bean名稱。每當我們使用此限定名時,Spring都會將具有該限定名的bean注入目標Bean中。
5.驗證Bean名稱
到目前為止,我們已經看到了不同的示例來演示如何給Spring bean命名。現在的問題是,我們如何驗證或測試這一點?
讓我們看一下驗證該行為的單元測試:
@ExtendWith(SpringExtension.class)
public class SpringBeanNamingUnitTest {
private AnnotationConfigApplicationContext context;
@BeforeEach
void setUp() {
context = new AnnotationConfigApplicationContext();
context.scan("com.baeldung.springbean.naming");
context.refresh();
}
@Test
void givenMultipleImplementationsOfAnimal_whenFieldIsInjectedWithQualifiedName_thenTheSpecificBeanShouldGetInjected() {
PetShow petShow = (PetShow) context.getBean("petShow");
assertThat(petShow.getCat().getClass()).isEqualTo(Cat.class);
assertThat(petShow.getDog().getClass()).isEqualTo(Dog.class);
}
在此JUnit測試中,我們將setUp
AnnotationConfigApplicationContext
,該方法用於獲取Bean。
然後,我們只需使用標準斷言來驗證Spring bean的類。
六,結論
在這篇快速文章中,我們研究了默認和自定義的Spring bean命名策略。
我們還了解了自定義Spring bean命名在需要管理多個相同類型bean的用例中如何有用。