我正在尝试使用spring混合配置,但是我的示例无法运行

时间:2018-07-12 04:19:47

标签: spring

我应该如何实现我的功能,我知道xml比注释更好,但是没有人遇到这种情况吗?
界面

public interface TestSuper {
    void play();
}

Clss TestA

public class TestA implements TestSuper{
    @Bean(name = "test")
    public  TestSuper test(){
        return new TestA();
    }
    public void play() {
        System.out.println("testa");
    }
}

TestB类

public class TestB{

    private  TestSuper testSupper;

    public TestB(TestSuper testSupper){
        this.testSupper=testSupper;
    }

    public void play() {
        testSupper.play();
    }

    public static void main(String[] args) {
        ApplicationContext ac= new ClassPathXmlApplicationContext("ts.xml");
        TestB b= (TestB) ac.getBean("testb");
        b.play();
    }
}

XmlConfig

<bean id="testb" class="com.jufu.TestB">
    <constructor-arg ref="test"/>
</bean>

错误消息

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testb' defined in class path resource [ts.xml]: Cannot resolve reference to bean 'test' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'test' available

0 个答案:

没有答案