Getting Error ( Field xyzRepository (that extends JPA Repository) in abcService class required a bean of xyzRepository that could not be found

huangapple 未分类评论46阅读模式
英文:

Getting Error ( Field xyzRepository (that extends JPA Repository) in abcService class required a bean of xyzRepository that could not be found

问题

以下是您提供的代码的翻译:

sipRepository.java

  1. import sip.sipDemo.model.mysql.SipEntity;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.stereotype.Repository;
  5. @Component
  6. @Repository
  7. public interface SipRepository extends JpaRepository<SipEntity,Integer>{
  8. }

SipEntity.java

  1. package sip.sipDemo.model.mysql;
  2. import javax.persistence.*;
  3. import java.sql.Date;
  4. import java.sql.Timestamp;
  5. @Entity
  6. @Table(name = "sip_table")
  7. public class SipEntity {
  8. public SipEntity() {}
  9. @Id
  10. @Column(name = "id")
  11. @GeneratedValue(strategy = GenerationType.AUTO)
  12. private int id;
  13. @Column(name = "userid")
  14. private int UserId;
  15. @Column(name = "folioNo")
  16. private String FolioNo;
  17. @Column(name = "amount")
  18. private double Amount;
  19. @Column(name = "isin")
  20. private String IsIn;
  21. @Column(name = "schemename")
  22. private String SchemeName;
  23. @Column(name = "nextSipDate")
  24. private Date NextSipDate;
  25. @Column(name = "status")
  26. private String status;
  27. }

SipController.java

  1. package sip.sipDemo.Controller;
  2. import sip.sipDemo.Service.SipService;
  3. import sip.sipDemo.model.mysql.SipEntity;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import java.util.List;
  10. import java.util.Optional;
  11. @Controller
  12. public class SipController {
  13. @Autowired
  14. private SipService sipservice;
  15. @ResponseBody
  16. @GetMapping(value="/")
  17. public String home()
  18. {
  19. return "Hello";
  20. }
  21. @ResponseBody
  22. @GetMapping(value = "/getAllEntities")
  23. public List<SipEntity> getAllEntities()
  24. {
  25. List<SipEntity> newList = sipservice.getAllEntities();
  26. return newList;
  27. }
  28. }

SipService.java

  1. package sip.sipDemo.Service;
  2. import sip.sipDemo.model.mysql.SipEntity;
  3. import sip.sipDemo.repository.mysql.SipRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. import java.util.Optional;
  8. @Service
  9. public class SipService {
  10. @Autowired
  11. private SipRepository sipRepository;
  12. public List<SipEntity> getAllEntities()
  13. {
  14. List<SipEntity> sipList = sipRepository.findAll();
  15. return sipList;
  16. }
  17. }

sipDemoApplication.java(主文件)

  1. package sip.sipDemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. @EnableAutoConfiguration
  8. @ComponentScan(basePackages = {"sip.sipDemo"})
  9. @SpringBootApplication
  10. public class SipDemoApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(SipDemoApplication.class, args);
  13. }
  14. }

您提供的错误信息是:

  1. : APPLICATION FAILED TO START
  2. ***************************
  3. Description:
  4. Field sipRepository in sip.sipDemo.Service.SipService required a bean of type 'sip.sipDemo.repository.mysql.SipRepository' that could not be found.
  5. The injection point has the following annotations:
  6. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  7. Action:
  8. Consider defining a bean of type 'sip.sipDemo.repository.mysql.SipRepository' in your configuration.
  9. > Task :SipDemoApplication.main() FAILED

可能的错误来源是:

  1. 检查您的包扫描路径是否正确。根据您提供的目录结构,您的主应用程序类(SipDemoApplication.java)位于sip.sipDemo包中。确保包扫描路径设置正确,以便Spring能够找到您的组件和存储库。
  2. 确保您的存储库接口(SipRepository.java)位于与主应用程序类相同的包或其子包中。Spring Data JPA需要正确扫描这些接口。
  3. 确保您的数据库配置正确。检查application.properties文件中的数据库连接信息,包括数据库URL、用户名和密码。
  4. 确保您的Maven或Gradle依赖项正确。如果您使用了Spring Boot Starter Data JPA,则应该包含必要的依赖项以支持JPA和自动配置。

如果上述检查都没有解决问题,请提供更多关于您的项目配置和依赖项的信息,以便更详细地诊断问题。

英文:

sipRepository.java

  1. import sip.sipDemo.model.mysql.SipEntity;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.stereotype.Repository;
  5. @Component
  6. @Repository
  7. public interface SipRepository extends JpaRepository&lt;SipEntity,Integer&gt;{
  8. }

SipEntity.java

  1. package sip.sipDemo.model.mysql;
  2. import javax.persistence.*;
  3. import java.sql.Date;
  4. import java.sql.Timestamp;
  5. @Entity
  6. @Table(name = &quot;sip_table&quot;)
  7. public class SipEntity {
  8. public SipEntity() {}
  9. @Id
  10. @Column(name = &quot;id&quot;)
  11. @GeneratedValue(strategy = GenerationType.AUTO)
  12. private int id;
  13. @Column(name = &quot;userid&quot;)
  14. private int UserId;
  15. @Column(name = &quot;folioNo&quot;)
  16. private String FolioNo;
  17. @Column(name = &quot;amount&quot;)
  18. private double Amount;
  19. @Column(name = &quot;isin&quot;)
  20. private String IsIn;
  21. @Column(name = &quot;schemename&quot;)
  22. private String SchemeName;
  23. @Column(name = &quot;nextSipDate&quot;)
  24. private Date NextSipDate;
  25. @Column(name = &quot;status&quot;)
  26. private String status;
  27. }

SipController.java

  1. package sip.sipDemo.Controller;
  2. import sip.sipDemo.Service.SipService;
  3. import sip.sipDemo.model.mysql.SipEntity;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import java.util.List;
  10. import java.util.Optional;
  11. @Controller
  12. public class SipController {
  13. @Autowired
  14. private SipService sipservice;
  15. @ResponseBody
  16. @GetMapping(value=&quot;/&quot;)
  17. public String home()
  18. {
  19. return &quot;Hello&quot;;
  20. }
  21. @ResponseBody
  22. @GetMapping(value = &quot;/getAllEntities&quot;)
  23. public List&lt;SipEntity&gt; getAllEntities()
  24. {
  25. List&lt;SipEntity&gt; newList = sipservice.getAllEntities();
  26. return newList;
  27. }
  28. }

SipService.java

  1. package sip.sipDemo.Service;
  2. import sip.sipDemo.model.mysql.SipEntity;
  3. import sip.sipDemo.repository.mysql.SipRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. import java.util.Optional;
  8. @Service
  9. public class SipService {
  10. @Autowired
  11. private SipRepository sipRepository;
  12. public List&lt;SipEntity&gt; getAllEntities()
  13. {
  14. List&lt;SipEntity&gt; sipList = sipRepository.findAll();
  15. return sipList;
  16. }
  17. }

sipDemoApplication.java (main file)

  1. package sip.sipDemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. @EnableAutoConfiguration
  8. @ComponentScan(basePackages = {&quot;sip.sipDemo&quot;})
  9. @SpringBootApplication
  10. public class SipDemoApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(SipDemoApplication.class, args);
  13. }
  14. }

The Error Message looks like

  1. : APPLICATION FAILED TO START
  2. ***************************
  3. Description:
  4. Field sipRepository in sip.sipDemo.Service.SipService required a bean of type &#39;sip.sipDemo.repository.mysql.SipRepository&#39; that could not be found.
  5. The injection point has the following annotations:
  6. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  7. Action:
  8. Consider defining a bean of type &#39;sip.sipDemo.repository.mysql.SipRepository&#39; in your configuration.
  9. &gt; Task :SipDemoApplication.main() FAILED

application.properties

  1. spring.datasource.url= jdbc:mysql://localhost:3306/sys?autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull
  2. spring.datasource.username=root
  3. spring.datasource.password=******** (actual password)
  4. spring.jpa.hibernate.ddl-auto=update
  5. spring.jpa.properties.org.hibernate.envers.audit_table_suffix=_AUDIT_LOG
  6. spring.jpa.show-sql=true
  7. spring.jpa.properties.hibernate.format_sql=true
  8. spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
  1. I have used mysql database.
  2. The main class is in the outer most layer and the packages are well structured.
  1. │&#160;&#160; │&#160;&#160; └── sipDemo
  2. │&#160;&#160; │&#160;&#160; ├── Controller
  3. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── SipController.java
  4. │&#160;&#160; │&#160;&#160; ├── model
  5. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── mysql
  6. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── SipEntity.java
  7. │&#160;&#160; │&#160;&#160; ├── repository
  8. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── mysql
  9. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── SipRepository.java
  10. │&#160;&#160; │&#160;&#160; ├── Service
  11. │&#160;&#160; │&#160;&#160; │&#160;&#160; └── SipService.java
  12. │&#160;&#160; │&#160;&#160; └── SipDemoApplication.java
  13. │&#160;&#160; └── resources
  14. │&#160;&#160; ├── application.properties
  15. │&#160;&#160; ├── static
  16. │&#160;&#160; └── templates
  1. If I remove @Autowire for the sipRepository object in sipService, the controller endpoints for sipRepository do not work but the rest is working fine.
  2. Please provide the possible source of error.

huangapple
  • 本文由 发表于 2020年7月23日 17:18:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63050918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定