Spring Boot Constructor Injection Error: Multiple Beans Found for Parameter
This error message indicates that the constructor of the class 'com.yimeng.shop.search.task.DataSyncService' expects a single bean as an argument for its first parameter, but there are two beans available in the application context. To fix this, you need to determine which bean should be used and make adjustments to ensure only one is available.
Here are potential solutions:
-
Remove one of the beans: If one bean isn't needed or shouldn't be used for this parameter, remove it from the application context or disable its creation.
-
Qualify the bean: If both beans are needed for different purposes, use the @Qualifier annotation to specify which bean should be used for the parameter. For example:
@Autowired
@Qualifier("myBean1")
private MyService myService;
-
Use @Primary annotation: If one bean should always be the default for the parameter, annotate it with @Primary. This tells Spring to use it when multiple candidates exist.
-
Specify the bean manually: If the above options aren't suitable, manually specify the bean using @Resource or @Autowired with a specific bean name or qualifier.
Once the necessary changes are made, rebuild and rerun the application to resolve the error.
原文地址: https://www.cveoy.top/t/topic/pWNS 著作权归作者所有。请勿转载和采集!