Spring @Primary注解示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(4.3k)|赞(0)|评价(0)|浏览(687)

在这篇文章中,我们将讨论Spring的@Primary注解,它是在框架的3.0版本中引入的。

简单地说,当有多个相同类型的Bean时,我们使用@Primary来给一个Bean更高的优先权。
让我们来详细描述一下这个问题。

为什么需要@Primary?

在某些情况下,我们需要注册超过一个相同类型的Bean。

在这个例子中,我们有mySQLConnection()oracleConnection()类型的豆子。

  1. @Configuration
  2. public class Config {
  3. @Bean
  4. public Connection mySQLConnection() {
  5. return new MySQLConnection();
  6. }
  7. @Bean
  8. public Connection oracleConnection() {
  9. return new OracleConnection();
  10. }
  11. }

如果我们试图运行该应用程序,Spring会抛出NoUniqueBeanDefinitionException
为了访问具有相同类型的Bean,我们通常使用@Qualifier("beanName")注解。

我们在注入点和@Autowired一起应用。在我们的例子中,我们在配置阶段选择了Bean,所以@Qualifier不能在这里应用。我们可以通过下面的链接了解更多关于@Qualifier注解的信息。
为了解决这个问题,Spring提供了@Primary注解。下面的例子展示了如何在spring应用程序中使用@Primary注解。

@Primary注解可用于任何直接或间接使用@Component注解的类,或使用@Bean注解的工厂方法。在这个例子中,我们将使用@Primary注解与@Component注解。

Spring @Primary注解示例

让我们创建一个例子来演示在Spring应用程序中使用@Primary注解的用法。

使用的工具和技术

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8或更高版本
  • Maven - 3.2以上
  • IDE - Eclipse Mars/STS

创建一个简单的Maven项目

使用你喜欢的IDE创建一个简单的Maven项目,关于打包结构请参考下面的章节。如果你是maven新手,请阅读本文《如何创建一个简单的Maven项目》。

项目结构

下图显示了项目结构,供您参考。

the pom.xml File

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.javaguides.spring</groupId>
  6. <artifactId>spring-primary-annotation</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>spring-scope-example</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-context</artifactId>
  18. <version>5.1.0.RELEASE</version>
  19. </dependency>
  20. </dependencies>
  21. <build>
  22. <sourceDirectory>src/main/java</sourceDirectory>
  23. <plugins>
  24. <plugin>
  25. <artifactId>maven-compiler-plugin</artifactId>
  26. <version>3.5.1</version>
  27. <configuration>
  28. <source>1.8</source>
  29. <target>1.8</target>
  30. </configuration>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. </project>

接下来,考虑以下MessageService接口。

MessageService.java

  1. package net.javaguides.spring.primary;
  2. public interface MessageService {
  3. public void sendMsg();
  4. }

创建三个名为FacebookMessageServiceEmailMessageServiceTwitterMessageService的bean,实现MessageService接口。

FacebookMessageService.java

  1. package net.javaguides.spring.primary;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class FacebookMessageService implements MessageService {
  5. @Override
  6. public void sendMsg() {
  7. System.out.println("FacebookMessageService implementation here");
  8. }
  9. }

EmailMessageService.java

  1. package net.javaguides.spring.primary;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class EmailMessageService implements MessageService {
  5. @Override
  6. public void sendMsg() {
  7. System.out.println("EmailMessageService Implementation here");
  8. }
  9. }

TwitterMessageService.java

  1. package net.javaguides.spring.primary;
  2. import org.springframework.context.annotation.Primary;
  3. import org.springframework.stereotype.Component;
  4. @Primary
  5. @Component
  6. public class TwitterMessageService implements MessageService {
  7. @Override
  8. public void sendMsg() {
  9. System.out.println("TwitterMessageService Implementation here");
  10. }
  11. }

注意,在上面的TwitterMessageService类中,我们添加了@Primary@Component注解。

基于注解的配置 - AppConfig.java

  1. package net.javaguides.spring.primary;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @ComponentScan(basePackages = "net.javaguides.spring.primary")
  6. public class AppConfig {
  7. }

运行中的Spring应用程序 - Application.java

让我们创建一个主类并运行一个应用程序。

  1. package net.javaguides.spring.primary;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. public class Application {
  4. public static void main(String[] args) {
  5. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  6. MessageService messageService = context.getBean(MessageService.class);
  7. messageService.sendMsg();
  8. context.close();
  9. }
  10. }

输出

  1. TwitterMessageService Implementation here

本文的源代码可在我的GitHub存储库中找到https://github.com/RameshMF/spring-core-tutorial

相关文章