加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring @Autowired无法连接Jpa存储库

发布时间:2020-12-15 01:25:43 所属栏目:大数据 来源:网络整理
导读:我在这里明显遗漏了什么. 我正在制作一个简单的弹簧启动应用程序,其中包含弹簧数据jpa并面临以下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [locassa.domain.repository.PersonReposi

我在这里明显遗漏了什么.
我正在制作一个简单的弹簧启动应用程序,其中包含弹簧数据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


控制器:

@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

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

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

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

    public static void main(String[] args) {

        SpringApplication.run(Application.class,args);

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读