我正在尝试在此项目中注入Repository bean,但不断获取:
引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.todo.services.TaskReader org.todo.services.AbstractTest.taskReader;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[org.todo.services.TaskReader]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ javax.inject.Inject()}
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.todo.services.TaskReader]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ javax.inject.Inject()}
java
org.todo
model
repository
TaskRepository
services
TaskReader
impl
TaskReaderImpl
resources
config
applicationContext.cfg.xml
persistence.cfg.xml
META-INF
persistence.xml
test
org.todo.services
AbstractTest
public interface TaskReader {
TaskItem findOne(long id);
List<TaskItem> findByOwner(ToDoUser toDoUser);
List<TaskItem> findAll();
}
@Service
@Transactional(readOnly = true)
public class TaskReaderImpl implements TaskReader {
@Resource
private TaskRepository taskRepository;
@Inject
private UserReader userReader;
...
@Repository
public interface TaskRepository extends CrudRepository<Task, Long> {
@Query("select t from tasks t where t.owner = :user order by t.done, t.startedDate")
List<Task> findAllByOwner(@Param("user") User user);
@Query("select t from tasks t order by t.done, t.startedDate")
List<Task> findAll();
@Query("delete from tasks t where t.owner= :user")
void deleteAllByOwner(@Param("user") User user);
}
我在某些测试中使用TaskReader
:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:config/applicationContext.cfg.xml")
public class AbstractTest {
@Inject
private TaskReader taskReader;
@Inject
private TaskWriter taskWriter;
@Inject
private UserWriter userWriter;
@Inject
private UserReader userReader;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--<context:annotation-config/>-->
<mvc:annotation-driven/>
<import resource="persistence.cfg.xml"/>
<context:component-scan base-package="org.todo.controller"/>
<context:component-scan base-package="org.todo.utils"/>
<context:component-scan base-package="org.todo.services"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<jpa:repositories base-package="org.todo.model.repository"/>
<bean id="jpaVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2"/>
</bean>
<jdbc:embedded-database id="dataSource" type="H2"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
<property name="rollbackOnCommitFailure" value="true"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="to-do-list"/>
<property name="jpaVendorAdapter" ref="jpaVendor"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
</beans>
现在我被赋予了autowired
bean的错误,但我找不到这个错误!
Listener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 在org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 在org.junit.runner.JUnitCore.run(JUnitCore.java:157) 在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202) 在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.todo.services.TaskReader org.todo.services.AbstractTest.taskReader;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[org.todo.services.TaskReader]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ javax.inject.Inject()} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) ......还有24个 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.todo.services.TaskReader]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ javax.inject.Inject()} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) ......还有26个 11:55:10,850 DEBUG ProfileValueUtils:68 - 检索到@PtProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 在org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 在org.junit.runner.JUnitCore.run(JUnitCore.java:157) 在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202) 在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.todo.services.TaskReader org.todo.services.AbstractTest.taskReader;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[org.todo.services.TaskReader]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ javax.inject.Inject()} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) ......还有24个 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.todo.services.TaskReader]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ javax.inject.Inject()} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) ......还有26个 11:55:10,894 DEBUG ProfileValueUtils:68 - 为测试类[org.todo.services.TaskServicesTest]检索@ProfileValueSourceConfiguration [null] 11:55:10,895 DEBUG ProfileValueUtils:80 - 检索到Profile11:55:11,135 TRACE TestContextManager:427 - afterTestClass():class [class org.todo.services.TaskServicesTest] 11:55:11,137 DEBUG DirtiesContextTestExecutionListener:138 - 测试类之后:context [[TestContext @ 1974736 testClass = TaskServicesTest,testInstance = [null],testMethod = [null],testException = [null],mergedContextConfiguration = [MergedContextConfiguration @ c44deb testClass = TaskServicesTest,locations =&#39; {classpath *:config / applicationContext.cfg.xml}&#39;,classes =&#39; {}&#39;,contextInitializerClasses =&#39; []&#39; ,activeProfiles =&#39; {}&#39;,contextLoader =&#39; org.springframework.test.context.support.DelegatingSmartContextLoader&#39;,parent = [null]]]],dirtiesContext [false]。 11:55:11,155 INFO GenericApplicationContext:1042 - 关闭org.springframework.context.support.GenericApplicationContext@c1d5a3:启动日期[Mon Jan 27 11:55:10 EET 2014];上下文层次结构的根 11:55:11,156 TRACE GenericApplicationContext:332 - 在org.springframework.context.support.GenericApplicationContext@c1d5a3中发布事件:org.springframework.context.event.ContextClosedEvent [source=org.springframework.context.support.GenericApplicationContext@c1d5a3:startup日期[Mon Jan 27 11:55:10 EET 2014];上下文层次结构的根] 11:55:11,158 DEBUG DefaultListableBeanFactory:246 - 返回单例bean的缓存实例&#39; lifecycleProcessor&#39; 11:55:11,159 INFO DefaultListableBeanFactory:444 - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@7c49e3中销毁单例:定义bean [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];工厂层级的根 断开与目标VM的连接,地址:&#39; 127.0.0.1:55631&#39;,transport:&#39; socket&#39;
项目来源:github。