我有以下文件:
public interface ProjectRepository extends JpaRepository<Project, Long> {
@Query("select p from Project p where p.projectName = ?1")
Project findByProjectName(String projectName);
}
<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>
@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;
}
}
我收到此自动回复错误:
DEBUG [main](InjectionMetadata.java:85) - 处理bean的注入方法&#39; com.xxx.hcs.persistence.repository.ProjecRepositoryTest&#39;:com.xxx.hcs.persistence.repository的AutowiredFieldElement .ProjectRepository com.xxx.hcs.persistence.repository.ProjecRepositoryTest.repository ERROR [main](TestContextManager.java:322) - 允许TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@60c9e03]准备测试实例时发生异常[com.xxx.hcs.persistence.repository.ProjecRepositoryTest@351037b ] org.springframework.beans.factory.BeanCreationException:创建名称为&#39; com.xxx.hcs.persistence.repository.ProjecRepositoryTest&#39;的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.xxx.hcs.persistence.repository.ProjectRepository com.xxx.hcs.persistence.repository.ProjecRepositoryTest.repository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.xxx.hcs.persistence.repository.ProjectRepository]的限定bean用于依赖:预期至少有1个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
项目结构如下:
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
我必须做一些明显错误的事情。请帮忙。
这是我遵循的步骤:http://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/