请问一下,我这样写有什么问题嘛?为什么通过方法注入找不到beanId?

来源:4-3 通过注解设定Bean的作用域

慕丝6064964

2019-08-04 22:55

@Configuration
@ComponentScan("com.qin5")
public class TestConfiguration5 {

}


@Component(value = "testBean15")
@Scope("singleton")
public abstract class TestBean5 {
    @Lookup
    public abstract AnotherBean5 createAnotherBean5();

    public void printBean() {
        System.out.println(createAnotherBean5());
    }
}

@Component
@Scope("prototype")
public class AnotherBean5 {
}
@Test
public void test5() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration5.class);
    TestBean5 testBean1 = context.getBean("testBean15", TestBean5.class);
    testBean1.printBean();
    testBean1.printBean();
    testBean1.printBean();
    testBean1.printBean();
    testBean1.printBean();
    testBean1.printBean();

}

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testBean15' is defined	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:702)	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)	at com.qin.BeanTest.test5(BeanTest.java:78)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)	at org.junit.runners.ParentRunner.run(ParentRunner.java:412)	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


写回答 关注

1回答

  • yao314
    2019-08-05 11:33:09
    TestBean5  这个类是否在你@ComponentScan("com.qin5") 扫描的包路径下呢?

    慕丝6064...

    在的呀.

    2019-08-05 11:58:48

    共 1 条回复 >

Spring框架小白的蜕变

Spring Ioc和Spring Bean基础内容的讲解

35278 学习 · 117 问题

查看课程

相似问题