有了这个课程,我很乐意用库junit 4.4执行一些测试。没有成功
public class GeolocationServiceTest extends AbstractAnnotationAwareTransactionalTests {
private static final String GEOJSON_FILE = "geojson_demo.geojson";
private static final String GEOJSON_FILE2 = "geojson_ecat.geojson";
private static final String ADDRESS = "rue commerce+PARIS";
private static final String ADDRESS2 = "Gravilliers 12, Paris";
private static final String[] CONFIG_LOCATIONS = new String[] {
"classpath:com/devices/testServiceContext.xml",
"classpath:com/devices/testApplicationContext.xml", "classpath:com/devices/testDatabaseMessageSource.xml", "classpath:com/devices/propertyeditorsContext.xml" };
@Autowired
private CompanyDao companyDao;
@Autowired
private GeolocationService geolocationService;
...
}
我得到了一个奇怪的错误,说注入一个bean是不可能的,但是这个bean存在于testServiceContext.xml文件中,所以我根本不理解它。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.devices.services.GeolocationServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devices.services.geolocation.GeolocationService com.devices.services.GeolocationServiceTest.geolocationService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:210)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:184)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:103)
at junit.framework.TestCase.runBare(TestCase.java:139)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:79)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:74)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:179)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:287)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:258)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:176)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.devices.services.geolocation.GeolocationService com.devices.services.GeolocationServiceTest.geolocationService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 25 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.devices.services.geolocation.GeolocationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 27 more
here you can see the file testServiceContext.xml:
<!-- Scan for services -->
<context:component-scan base-package="com.devices.services.impl" />
<context:component-scan base-package="com.devices.services.geolocation" />
并且该类似乎声明得很好
package com.devices.services.geolocation
@Service(value = "geolocationService")
public interface GeolocationService {
....
}
答案 0 :(得分:1)
您将在以下情况中收到此例外:
@Service(value = "geolocationService")
public interface GeolocationService {
....
}
现在您已经创建了上面的接口
的实现public class GeolocationServiceImpl implements GeolocationService {
..........
}
现在,如果您尝试通过@Autowired注入GeolocationService,将收到异常:
No qualifying bean of type [com.devices.services.geolocation.GeolocationService] found for dependency
解决方案:
1)将@Component注释放在GeolocationServiceImpl上,如下所示:
@Component
public class GeolocationServiceImpl implements GeolocationService {
..........
}
2)在spring-context.xml中将其定义为: -
<bean id="geolocationService" class="com.devices.services.geolocation.GeolocationServiceImpl" />
总之,您需要让Spring ApplicationContext了解您的接口的实现。