相关疑难解决方法(0)

使用@DataJpaTest进行Spring测试不能使用@Repository自动装配类(但是使用接口存储库工作!)

我试图理解为什么我不能自动装配类库,但我可以在相同的包中自动装配一个接口存储库以进行相同的测试.当我启动应用程序时,相同的存储库按预期工作.

一,错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.app.person.repository.PersonRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.raiseNoMatchingBeanFound(DefaultPersonbleBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.doResolveDependency(DefaultPersonbleBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.resolveDependency(DefaultPersonbleBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 28 more
Run Code Online (Sandbox Code Playgroud)

我有一个非常简单的例子.考试:

@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryTest {

    @Autowired
    private PersonRepository personRepository; // fail...

    @Autowired
    private PersonCrudRepository personCrudRepository; // works!

    @Test
    public void findOne() {
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库类:

@Repository
public class PersonRepository {
    //code
}
Run Code Online (Sandbox Code Playgroud)

存储库界面:

@Repository
public interface PersonCrudRepository extends …
Run Code Online (Sandbox Code Playgroud)

java spring integration-testing spring-data-jpa

9
推荐指数
2
解决办法
7807
查看次数

Spring Boot似乎找不到存储库

我不确定我的配置有什么问题,但是据我所知它应该是正确的,尽管那个春天宣布没有这样的bean定义。我对以下几点表示赞同:

  • 配置类位于其他所有内容的根包中
  • 该存储库扩展了JpaRepository,因此不需要注释
  • 播放器对象带有“ @Entity”注释
  • 此存储库的pojo根据注释完全映射
  • 没有非Java配置文件
  • 数据库已建立,并使用了正确的凭据。
  • 出于开发目的,Jpa / hibernate设置为“创建-更新”。

这是我完整的调试日志: spring启动日志pastebin

没有调试标志的日志:

2017-06-12 09:34:36.395  INFO 12880 --- [  restartedMain] 
d.a.c.newworlds.NewWorldsApplication     : No active profile set, falling back to default profiles: default
2017-06-12 09:34:36.727  INFO 12880 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10057b9b: startup date [Mon Jun 12 09:34:36 CEST 2017]; root of context hierarchy
2017-06-12 09:34:38.090  INFO 12880 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'playerRepository' with a different definition: replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; …
Run Code Online (Sandbox Code Playgroud)

java spring maven spring-boot

5
推荐指数
1
解决办法
5923
查看次数

无法自动接线。找不到“存储库”类型的bean

我正在使用Spring Data Jpa,这是我的项目结构:

App
  ConfigPackage
    MyConfig
  ServicePackage
    MyService
  RepositoryPackage
    MyRepository
Run Code Online (Sandbox Code Playgroud)

这是MyRepository

public interface MyRepository extends JpaRepository<MyEntity, Long> {
}
Run Code Online (Sandbox Code Playgroud)

这是MyService

@Service
public class MyService {

    @Autowired
    private MyRepository myRepository; <---here

    ...
}
Run Code Online (Sandbox Code Playgroud)

这是MyConfig

@Configuration
@EnableJpaRepositories(
        basePackages = "RepositoryPackage",
        entityManagerFactoryRef = "xxx",
        transactionManagerRef = "xxx"
)
public class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)

我用@Autowired注入MyRepositoryMyService,但的IntelliJ总是抱怨

无法自动接线。找不到“ MyRepository”类型的bean

即使代码可以编译并成功运行。

为什么IntelliJ无法识别这不是错误?如何清除IntelliJ的警告?

IntelliJ版本:2018.2.6

java spring intellij-idea spring-data-jpa spring-boot

5
推荐指数
1
解决办法
4852
查看次数

Repository annotation is not working on Spring data JPA

I am building a Spring-boot application where I am using Spring data jpa feature.

Please find below my dao layer code

package com.adv.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerDao extends JpaRepository<Customer, String> {

}
Run Code Online (Sandbox Code Playgroud)

I am using a DaoProvider class as follows:

package com.adv.dao;

import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DaoProvider implements Serializable {

private static final long serialVersionUID = 1L; 

@Autowired
private CustomerDao customerDao;

public CustomerDao getCustomerDao() {
    return customerDao;
}
}
Run Code Online (Sandbox Code Playgroud)

My spring …

java spring spring-data-jpa spring-boot

1
推荐指数
1
解决办法
8950
查看次数