Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring JPA JUnit测试自动连线失败_Java_Spring_Hibernate_Jpa - Fatal编程技术网

Java Spring JPA JUnit测试自动连线失败

Java Spring JPA JUnit测试自动连线失败,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我有以下文件: ProjectRepository.java 我收到此自动连线错误: 调试[main](InjectionMetadata.java:85)-处理bean'com.xxx.hcs.persistence.repository.ProjecRepositoryTest'的注入方法:com.xxx.hcs.persistence.repository.ProjectRepository com.xxx.hcs.persistence.repository.ProjecReposito

我有以下文件:

ProjectRepository.java 我收到此自动连线错误:

调试[main](InjectionMetadata.java:85)-处理bean'com.xxx.hcs.persistence.repository.ProjecRepositoryTest'的注入方法:com.xxx.hcs.persistence.repository.ProjectRepository com.xxx.hcs.persistence.repository.ProjecRepositoryTest.repository的AutowiredFeldElement 错误[main](TestContextManager.java:322)-在允许TestExecutionListener[org.springframework.test.context.support]时捕获异常。DependencyInjectionTestExecutionListener@60c9e03]准备测试实例[com.xxx.hcs.persistence.repository]。ProjecRepositoryTest@351037b] org.springframework.beans.factory.BeanCreationException:创建名为“com.xxx.hcs.persistence.repository.ProjecRepositoryTest”的bean时出错:自动关联依赖项的注入失败;嵌套异常为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个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.postProcessPropertyValues(AutoWiredNotationBeanPostProcessor.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
我一定是做错了什么。请帮忙


这是我遵循的步骤:

问题是你没有正确加载配置文件,你能发布你的项目结构吗?你留下了
@Query
注释。这是多余的。我刚刚将项目结构try change
文件:*/…
添加到“classpath:db config.xml”,并确保在实体
project
中有一个名为
projectName
的属性,我将位置更改为classpath*:db config.xml,classpath*:persistence.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>
@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;
}
}
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