美文网首页程序员
因为父类、子类都有@Service注解而引起的NoUniqueB

因为父类、子类都有@Service注解而引起的NoUniqueB

作者: 李北北 | 来源:发表于2018-03-09 16:12 被阅读0次

启动spring boot项目时候发现了下面的异常

15:41:33.832 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.xxx.xxx.service.BaseRestService] is defined: expected single matching bean but found 2: baseRestServiceImpl,coreFeedbackServiceImpl
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:172) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at com.yuedu.release.start.Application.main(Application.java:52) [classes/:na]
2018-03-09 15:41:33.833 ERROR 45752 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field baseRestService in com.xxx.xxx.controller.BaseRestController required a single bean, but 2 were found:
    - baseRestServiceImpl: defined in file [E:\codeRep\xxx\service\impl\BaseRestServiceImpl.class]
    - coreFeedbackServiceImpl: defined in file [E:\codeRep\xxx\service\impl\CoreFeedbackServiceImpl.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

15:41:33.833 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

下面分别是异常信息中提到的BaseRestServiceImpl类、CoreFeedbackServiceImpl类代码

/**
 * 基础服务实现类
 * @author 北北
 * @date 2018年3月8日下午5:09:11
 */
@Service
public class BaseRestServiceImpl implements BaseRestService {
        
}
@Service
public class CoreFeedbackServiceImpl extends BaseRestServiceImpl implements CoreFeedbackService {

}

其中CoreFeedbackService接口继承BaseRestService接口。

解决办法是用@Primary注解指定CoreFeedbackServiceImpl为优先注入的实例,修改后的CoreFeedbackServiceImpl类代码如下:

@Primary
@Service
public class CoreFeedbackServiceImpl extends BaseRestServiceImpl implements CoreFeedbackService {

}

参考文章
spring5.0 之@Primary注解的应用

相关文章

  • 因为父类、子类都有@Service注解而引起的NoUniqueB

    启动spring boot项目时候发现了下面的异常 下面分别是异常信息中提到的BaseRestServiceImp...

  • @PostConstruct注解的继承问题

    子类重写父类带有@PostConstruct注解的方法,子类方法可以继承到@PostConstruct注解的效果。...

  • spring-data-jpa实体类继承抽象类如何映射父类的属性

    在抽象父类上加上注解@MappedSuperclass 子类直接继承抽象父类并加上@Entity注解并用@Tabl...

  • 注解继承

    接口注解不能被实现类继承。接口注解不能被子接口继承。父类接口能被子类接口继承,注解需要被@Inherited元注解...

  • 访问子类对象的实例变量

    子类的方法可以访问父类的实例变量,这是因为子类继承父类,就会获得父类的成员变量和成员方法,父类的方法不能访问子类的...

  • 2022-05-15程序员周记

    成员变量继承 Java中的子类和父类都有相同命名的变量,当左边的类是父类时则获取父类的变量值,若是子类则使用子类的...

  • 多态只针对方法,而不是属性

    java中子类只能重写父类的方法 ,而不能重写属性 创建两个类,一个子类一个父类进行测试 父类 子类先建一个和父类...

  • 26号c#总结

    26号 今天开始讲多态,在c和c++中都有涉及。继承是子类使用父类的方法,而多态则是父类使用子类的方法。重点内容是...

  • 调用被子类重写的方法

    在一般情况一下,子类可以调用父类的方法,因为子类继承父类,会获得父类的成员变量和方法,但父类不可以调用子类的方法,...

  • 86-OOP之子类调用父类方法

    如果子类和父类具有同名的方法,那么父类方法将被遮盖住。可以在子类中明确指明调用的是父类方法,而不是子类的同名方法。

网友评论

    本文标题:因为父类、子类都有@Service注解而引起的NoUniqueB

    本文链接:https://www.haomeiwen.com/subject/qhzpfftx.html