问题描述:

I have the following files:

ProjectRepository.java

public interface ProjectRepository extends JpaRepository<Project, Long> {

@Query("select p from Project p where p.projectName = ?1")

Project findByProjectName(String projectName);

}

db-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.xxx.hcs.persistence.repository" />

<context:component-scan base-package="com.xxx.hcs.persistence"/>

<!-- Working version of Oracle Database -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

p:driverClassName="oracle.jdbc.OracleDriver" p:url="jdbc:oracle:thin:@hrsdev.xxx.net:1548:hrsdev"

p:username="xxx" p:password="xxx" >

</bean>

<bean

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"

id="entityManagerFactory">

<property name="dataSource" ref="dataSource" />

</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"

id="transactionManager">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<context:annotation-config />

</beans>

ProjecRepositoryTest.java

@ContextConfiguration(locations={"file:**/db-config.xml,file:**/persistence.xml"})

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)

@Transactional

@RunWith(SpringJUnit4ClassRunner.class)

public class ProjecRepositoryTest {

public static Logger logger = LoggerFactory.getLogger(ProjectRepository.class);

@Autowired

ProjectRepository repository;

@Test

public void testProject() {

Project project = repository.findOne(46L);

assertTrue(project != null);

}

public void setRepository(ProjectRepository repository) {

this.repository = repository;

}

}

I am getting this Autowired error:

DEBUG [main] (InjectionMetadata.java:85) - Processing injected method of bean 'com.xxx.hcs.persistence.repository.ProjecRepositoryTest': AutowiredFieldElement for com.xxx.hcs.persistence.repository.ProjectRepository com.xxx.hcs.persistence.repository.ProjecRepositoryTest.repository

ERROR [main] (TestContextManager.java:322) - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]60c9e03] to prepare test instance [[email protected]]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.hcs.persistence.repository.ProjecRepositoryTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxx.hcs.persistence.repository.ProjectRepository com.xxx.hcs.persistence.repository.ProjecRepositoryTest.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xxx.hcs.persistence.repository.ProjectRepository] 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)

The project structure is like this:

hcs-persistence

src

main

java

com

xxx

hcs

persistence

repository

ProjectRepository.java

resource

db-config.xml

persistence.xml

test

java

com

xxx

hcs

persistence

repository

ProjectRepositoryTest.java

I must be doing something obviously wrong. Please help.

this is the steps I followed: http://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/

相关阅读:
Top