当前位置:  开发笔记 > 编程语言 > 正文

Spring @Autowired无法连接Jpa存储库

如何解决《Spring@Autowired无法连接Jpa存储库》经验,为你挑选了1个好方法。

我在这里明显遗漏了什么.我正在制作一个简单的spring boot应用程序,spring data jpa包含内容和面对错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [locassa.domain.repository.PersonRepository] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    ... 32 common frames omitted

我的代码:

应用:

@SpringBootApplication
@ComponentScan(basePackages = {"app.controller", "app.domain"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

的pom.xml



    4.0.0

    pl.mosek
    pl.mosek
    0.1-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.0.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            1.16.6
        

        
            com.h2database
            h2
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
    

    
        1.8
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


控制器:

@RestController
public class TestController {

    @Autowired
    PersonService personService;

    @RequestMapping("/")
    public String index() {
        return "Test spring boot";
    }

    @RequestMapping("/person/{id}")
    public Person personById(@PathVariable Long id) {
        return personService.findPerson(id);
    }
}

PersonService:

public interface PersonService {

    Person findPerson(Long id);
}

PersonServiceImpl:

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonRepository personRepository;

    public Person findPerson(Long id) {
       return personRepository.findOne(id);
    }
}

PersonRepository(这个不能自动装配):

public interface PersonRepository extends CrudRepository {
}

已在网上搜索过.我没找到一件事.有任何想法吗?



1> 小智..:

我也遇到了同样的问题.我用以下解决方案解决了这个问题.如果您的实体类和存储库位于不同的包中,则需要使用以下注释.

@SpringBootApplication
@EntityScan(basePackages = {"EntityPackage"} )
@EnableJpaRepositories(basePackages = {"RepositoryPackage"})
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }
}

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有