想要去撸串
2021-07-14 23:37
采纳率: 50%
浏览 407
已采纳

spring 抽象类实现接口,多个实现类继承抽象类,不能注入的问题

定义接口

public interface TestService {

    public void test1(int id);
}

定义抽象类,并且抽象类继承接口。实现接口方法,

public abstract class AbstractTest implements TestService {

    @Override
    public void test1(int id) {
        //此处一部分前置业务处理或者校验....
        System.out.printf("aaaaaaaaaaaaaaaaaaaa");
        
        String beanName = null;
        if(id == 1){
          beanName  = "testServiceImpl1";
        }else{
          beanName  = "testServiceImpl2";
        }
        AbstractTest abstractTest = (AbstractTest) SpringContextUtil.getBean(beanName);
        test2();
    }
    public abstract void test2();
}

定义实现类1,继承抽象,实现抽象方法

@Service(value = "testServiceImpl1")
public class TestServiceImpl1 extends AbstractTest {

    Logger logger = Logger.getLogger("TestServiceImpl");

    @Override
    public void test2() {
        logger.info("TestServiceImpl1-test2....");
    }
}

定义实现类2,继承抽象,实现抽象方法

@Service(value = "testServiceImpl2")
public class TestServiceImpl2 extends AbstractTest {

    Logger logger = Logger.getLogger("TestServiceImpl2");

    @Override
    public void test2() {
        logger.info("TestServiceImpl2-test2....");
    }
}

Test类进行测试

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestCaseService{

    @Autowired
    TestService testService;

    @Test
    public void test1(){
        testService.test1(1);
        System.out.printf("");
    }
}

报错如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.springauto.TestCaseService': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.Service.TestService' available: expected single matching bean but found 2: testServiceImpl1,testServiceImpl2

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:399)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.Service.TestService' available: expected single matching bean but found 2: testServiceImpl1,testServiceImpl2
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:220)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1358)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)
    ... 30 more

我的实现类可能是动态的,就是说根据不同的值,选择的实现类不一样,那么在注入@Qualifier()的时候,不能确定走那个实现类,需要在代码运行的时候,根据值动态决定。请问,这样有办法处理么?
怎么解决?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 走一步-再走一步 2021-07-15 08:02
    已采纳

    方法一: 指定bean的名字
    @Autowired
    TestService testServiceImpl1;
    方法二:

    @Autowired
    @Qualifier(value = "testServiceImpl1")
    TestService testService;

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(4条)

相关推荐 更多相似问题