【报错解决】expected single matching bean but found 2


我立志做一名把细节都说清楚的博主,欢迎关注🎉 ~
原创不易,有帮助还请鼓励个【赞】哦,谢谢无敌可爱帅气又迷人的小哥哥、小姐姐,爱你哦 ❥(^_-)~

报错信息展示:

Error creating bean with name '***ManagementController': Unsatisfied dependency expressed through field '***Service';
 
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'com.***.***.service.***Service' 

available: expected single matching bean but found 2: ***ServiceImpl,***ServiceImpl

项目背景:

基础的SSM框架项目。

报错还原:

实现类A

@Service
public class AxxxServiceImpl implements AxxxxService {
}

实现类B

@Service
public class BxxxServiceImpl implements BxxxService {
}

controller中注入

@Controller
@RequestMapping("1")
public class XxxxController {
    @Autowired
    //@Qualifier(value = "BxxxServiceImpl")
    private BxxxService bxxxService;

    @Autowired
    //@Qualifier(value = "AxxxServiceImpl")
    private AxxxService axxxService;

    @RequestMapping("/1")
    @ResponseBody
    private Map<String,Object> getXxxxInfo(){
    	// 略去
    }

然后启动服务,访问后报错,信息如下:

Error creating bean with name '***ManagementController': Unsatisfied dependency expressed through field '***Service';
 
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'com.***.***.service.***Service' 

available: expected single matching bean but found 2: ***ServiceImpl,***ServiceImpl

原因分析:

当一个接口实现,由两个实现类时,只使用@Autowired注解,会报错。

原因是存在两个实例Aservice,Bservice,系统不知道注入哪个一个实例。

解决方案:

方案一:使用@Qualifier注解来指明注入的实例。

@Controller
@RequestMapping("1")
public class XxxxController {
    @Autowired
    @Qualifier(value = "BxxxServiceImpl")
    private BxxxService bxxxService;

    @Autowired
    @Qualifier(value = "AxxxServiceImpl")
    private AxxxService axxxService;

    @RequestMapping("/1")
    @ResponseBody
    private Map<String,Object> getXxxxInfo(){
    	// 略去
    }

方案二:使用@Resource(name=“Xxxservice”)注解来指明注入的实例。

@Controller
@RequestMapping("1")
public class XxxxController {
    @Resource(name="BxxxServiceImpl")
    private BxxxService bxxxService;

    @Resource(name="AxxxServiceImpl")
    private AxxxService axxxService;

    @RequestMapping("/1")
    @ResponseBody
    private Map<String,Object> getXxxxInfo(){
    	// 略去
    }

补充说明:

网上还有说需要在实现类中加入注解@Component(value="XxxxServiceImpl ")才能实现,就我的解决过程中,以上步骤为止就已经解决问题了。

实现类A

@Component(value="AxxxServiceImpl ")
public class AxxxServiceImpl implements AxxxxService {
}

实现类B

@Component(value="BxxxServiceImpl ")
public class BxxxServiceImpl implements BxxxService {
}

我立志做一名把细节都说清楚的博主,欢迎关注🎉 ~
原创不易,有帮助还请鼓励个【赞】哦,谢谢无敌可爱帅气又迷人的小哥哥、小姐姐,爱你哦 ❥(^_-)~

拓展:@AutoWired、@Resource、@Qualifier理解

资料参考链接:
@Autowired注解与@Qualifier注解搭配使用.

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
### 回答1: springapplicationgetbean是Spring框架中的一个方法,用于从Spring容器中获取一个已经注册的bean对象。可以使用该方法获取任何类型的bean对象,包括自定义的bean对象和Spring内置的bean对象。 该方法的使用方法如下: 1. 在Spring Boot项目中创建一个SpringApplication对象 2. 使用SpringApplication对象获取一个ApplicationContext对象 3. 使用ApplicationContext对象调用getBean方法获取指定类型的bean对象 示例代码如下: ``` @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Autowired private MyBean myBean; @Bean public MyBean myBean() { return new MyBean(); } @GetMapping("/") public String home() { return myBean.getMessage(); } } public class MyBean { public String getMessage() { return "Hello World"; } } ``` 在上面的代码中,我们使用@Autowired注解将MyBean对象注入到DemoApplication类中,然后在home方法中调用MyBean对象的getMessage方法获取一个字符串。同时,我们也在DemoApplication中创建了一个名为myBean的Bean对象,并将其注册到Spring容器中。 如果需要获取其他类型的bean对象,只需要将MyBean替换为其他类的名字即可。 ### 回答2: Spring框架的`SpringApplication`是用于启动Spring应用程序的类。`getBean`是`SpringApplication`类的一个方法,用于从Spring容器中获取指定名称的bean对象。 在使用`getBean`方法之前,需要先创建一个`ApplicationContext`对象,该对象是Spring容器的实例,它负责管理和加载Spring应用程序中的所有bean。 `getBean`方法的用法是通过传入一个字符串参数,该参数指定了要获取的bean的名称。当调用`getBean`方法时,Spring容器会根据指定的名称在容器中查找对应的bean对象,并将其返回。 需要注意的是,`getBean`方法返回的是一个Object类型的对象,因此在使用时可能需要进行类型转换。 下面是一个示例代码,演示了如何使用`SpringApplication`类的`getBean`方法: ```java // 创建ApplicationContext对象 ApplicationContext context = SpringApplication.run(Application.class, args); // 根据bean的名称获取bean对象 Object bean = context.getBean("beanName"); // 将bean对象转换为指定类型 CustomBean customBean = (CustomBean) bean; // 对获取到的bean对象进行操作 customBean.doSomething(); ``` 这段代码首先创建了一个ApplicationContext对象,然后利用该对象的`getBean`方法获取了一个名为"beanName"的bean对象。接下来,将获取到的bean对象转换为自定义的类型`CustomBean`,并对该对象进行操作。 总之,`SpringApplication`的`getBean`方法使得我们可以方便地在Spring应用程序中获取已经创建好的bean对象,从而实现对bean的使用和操作。 ### 回答3: SpringApplication的getBean()方法是用来获取IOC容器中的bean实例的。 在Spring中,Bean是应用程序中最基本的对象,交给Spring容器进行管理。当应用程序需要访问或使用某个Bean时,可以使用getBean()方法进行获取。 getBean()方法的使用需要两个参数,分别是bean的名称和bean的类型。bean的名称可以是在配置文件中指定的名称,也可以是通过自动装配方式指定的名称;bean的类型可以是接口、抽象类、具体类等。 getBean()方法的底层操作是通过IOC容器来查找bean的实例对象。IOC容器是Spring框架的核心,它负责创建、管理和装配Bean实例。当调用getBean()方法时,IOC容器会根据指定的名称和类型,在容器中查找对应的Bean实例并返回。 如果找不到对应的Bean实例,getBean()方法会抛出异常。在这种情况下,可以通过添加@Nullable注解或使用Optional<T>来避免异常的抛出,将返回值设置为null或包装成Optional对象。 getBean()方法还支持对bean实例的作用域进行控制。在配置文件中,可以通过定义不同的作用域(scope)来控制bean的创建和销毁时机。常用的作用域有两个:singleton和prototype。singleton表示每个IOC容器中只有一个该类型的bean实例,prototype表示每次获取bean时都会创建一个新的实例。 总而言之,SpringApplication的getBean()方法是Spring框架提供的用于获取IOC容器中bean实例的方法,通过指定名称和类型,可以从IOC容器中获取到所需的Bean,提供了方便快捷的访问方式。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我梦Leo

谢谢无敌帅气可爱迷人的你哦 ~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值