SpringBoot中如何配置同一接口的多个实现类实例?

2026-04-30 05:552阅读0评论SEO问题
  • 内容介绍
  • 相关推荐

本文共计399个文字,预计阅读时间需要2分钟。

SpringBoot中如何配置同一接口的多个实现类实例?

在SpringBoot项目中,可能会遇到一个接口存在多个实现类的情况。如果不进行配置,当编译器不知道应该使用哪个实现类时,就会报错。这是因为需要配置来指定使用哪个实现类。以下是一个示例:

接口如下:javapublic interface MyInterface { void doSomething();}

实现类1:javapublic class MyInterfaceImpl1 implements MyInterface { @Override public void doSomething() { System.out.println(Impl1 doing something); }}

实现类2:javapublic class MyInterfaceImpl2 implements MyInterface { @Override public void doSomething() { System.out.println(Impl2 doing something); }}

在配置文件中指定使用哪个实现类:propertiesmy.interface.impl=MyInterfaceImpl1

或者在代码中指定:java@Configurationpublic class MyConfig { @Bean public MyInterface myInterface() { return new MyInterfaceImpl1(); }}

SpringBoot项目中可能出现一个接口有多个实现类的情况,如果不进行配置,注入接口时编译器不知道要注入哪个实现类就会报错,因此需要进行配置。以下进行举例:

接口如下:

public interface NoticeService { public String noticeUser(Long id); }

两个实现类如下:

@Service public class NoticeServiceImpl1 implements NoticeService { public String noticeUser(Long id){ return noticeServe1(id); } }

@Service public class NoticeServiceImpl2 implements NoticeService { public String noticeUser(Long id){ return noticeServe2(id); } }

Controller如下:

@Controller @RequestMapping("notice") public class NoticeController{ @Autowired NoticeService noticeService; }

这样直接启动后就会报错,需要在添加@Qualifier注解:

SpringBoot中如何配置同一接口的多个实现类实例?

@Controller @RequestMapping("notice") public class NoticeController{ @Autowired @Qualifier("impl1") NoticeService noticeService; }

需要调用的实现类中添加标识:

@Service("impl1") public class NoticeServiceImpl1 implements NoticeService { public String noticeUser(Long id){ return noticeServe1(id); } }

此时再运行即可

到此这篇关于SpringBoot同一接口多个实现类配置的文章就介绍到这了,更多相关springboot多个实现类配置内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计399个文字,预计阅读时间需要2分钟。

SpringBoot中如何配置同一接口的多个实现类实例?

在SpringBoot项目中,可能会遇到一个接口存在多个实现类的情况。如果不进行配置,当编译器不知道应该使用哪个实现类时,就会报错。这是因为需要配置来指定使用哪个实现类。以下是一个示例:

接口如下:javapublic interface MyInterface { void doSomething();}

实现类1:javapublic class MyInterfaceImpl1 implements MyInterface { @Override public void doSomething() { System.out.println(Impl1 doing something); }}

实现类2:javapublic class MyInterfaceImpl2 implements MyInterface { @Override public void doSomething() { System.out.println(Impl2 doing something); }}

在配置文件中指定使用哪个实现类:propertiesmy.interface.impl=MyInterfaceImpl1

或者在代码中指定:java@Configurationpublic class MyConfig { @Bean public MyInterface myInterface() { return new MyInterfaceImpl1(); }}

SpringBoot项目中可能出现一个接口有多个实现类的情况,如果不进行配置,注入接口时编译器不知道要注入哪个实现类就会报错,因此需要进行配置。以下进行举例:

接口如下:

public interface NoticeService { public String noticeUser(Long id); }

两个实现类如下:

@Service public class NoticeServiceImpl1 implements NoticeService { public String noticeUser(Long id){ return noticeServe1(id); } }

@Service public class NoticeServiceImpl2 implements NoticeService { public String noticeUser(Long id){ return noticeServe2(id); } }

Controller如下:

@Controller @RequestMapping("notice") public class NoticeController{ @Autowired NoticeService noticeService; }

这样直接启动后就会报错,需要在添加@Qualifier注解:

SpringBoot中如何配置同一接口的多个实现类实例?

@Controller @RequestMapping("notice") public class NoticeController{ @Autowired @Qualifier("impl1") NoticeService noticeService; }

需要调用的实现类中添加标识:

@Service("impl1") public class NoticeServiceImpl1 implements NoticeService { public String noticeUser(Long id){ return noticeServe1(id); } }

此时再运行即可

到此这篇关于SpringBoot同一接口多个实现类配置的文章就介绍到这了,更多相关springboot多个实现类配置内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!