Spring 注入失败的原因
有一次写代码如下
??ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
??HarborBusiness service = (HarborBusiness) ctx.getBean("harborBusiness");
?
总是报no bean named
?
查了半天没查出来了,后来把前一个版本的工程拿来,一点一点的加功能
终于发现了,根本和工程没有一点关系,是 ctx.getBean("harborBusiness"); 这里写错了,
HarborBusiness它本身是个接口类,而以前的测试都是? ctx.getBean("harborBusinessImpl");这样的
HarborBusinessImpl这个才是真正的实现类,spring的注入的语法也是在这个类里写的
?
晕了。。。呵。。。
?
?
?
?
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'harborBusiness' is defined
?at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
?at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
?at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
?at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
?at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
?at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
?at junit.com.gamesloc.bottle.business.impl.MsgBusinessImplTest.testAddMsg(MsgBusinessImplTest.java:14)
?at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
?at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
?at java.lang.reflect.Method.invoke(Method.java:597)
?at junit.framework.TestCase.runTest(TestCase.java:164)
?at junit.framework.TestCase.runBare(TestCase.java:130)
?at junit.framework.TestResult$1.protect(TestResult.java:106)
?at junit.framework.TestResult.runProtected(TestResult.java:124)
?at junit.framework.TestResult.run(TestResult.java:109)
?at junit.framework.TestCase.run(TestCase.java:120)
?at junit.framework.TestSuite.runTest(TestSuite.java:230)
?at junit.framework.TestSuite.run(TestSuite.java:225)
?at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
?at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
?at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
?at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
?at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
?at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
?
?
?