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

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

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

问题

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

sipRepository.java

import sip.sipDemo.model.mysql.SipEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component
@Repository
public interface SipRepository extends JpaRepository<SipEntity,Integer>{

}

SipEntity.java

package sip.sipDemo.model.mysql;

import javax.persistence.*;
import java.sql.Date;
import java.sql.Timestamp;

@Entity
@Table(name = "sip_table")
public class SipEntity {

      public SipEntity() {}
     @Id
     @Column(name = "id")
     @GeneratedValue(strategy = GenerationType.AUTO)
     private int id;

     @Column(name = "userid")
     private int UserId;

     @Column(name = "folioNo")
     private String FolioNo;

     @Column(name = "amount")
     private double Amount;

     @Column(name = "isin")
     private String IsIn;

     @Column(name = "schemename")
     private String SchemeName;

     @Column(name = "nextSipDate")
     private Date NextSipDate;

     @Column(name = "status")
     private String status;
}

SipController.java

package sip.sipDemo.Controller;
import sip.sipDemo.Service.SipService;
import sip.sipDemo.model.mysql.SipEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Optional;

@Controller
public class SipController {

     @Autowired
     private SipService sipservice;


    @ResponseBody
    @GetMapping(value="/")
    public String home()
    {
        return "Hello";
    }

    @ResponseBody
    @GetMapping(value = "/getAllEntities")
    public List<SipEntity> getAllEntities()
    {
         List<SipEntity> newList = sipservice.getAllEntities();

         return newList;
    }
}

SipService.java

package sip.sipDemo.Service;

import sip.sipDemo.model.mysql.SipEntity;
import sip.sipDemo.repository.mysql.SipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SipService {

    @Autowired
    private SipRepository sipRepository;

    public List<SipEntity> getAllEntities()
    {
        List<SipEntity> sipList = sipRepository.findAll();

        return sipList;
    }
}

sipDemoApplication.java(主文件)

package sip.sipDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableAutoConfiguration
@ComponentScan(basePackages = {"sip.sipDemo"})
@SpringBootApplication
public class SipDemoApplication {

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

}

您提供的错误信息是:

: APPLICATION FAILED TO START

    ***************************
    
    Description:
    
    Field sipRepository in sip.sipDemo.Service.SipService required a bean of type 'sip.sipDemo.repository.mysql.SipRepository' that could not be found.
    
    The injection point has the following annotations:
    	- @org.springframework.beans.factory.annotation.Autowired(required=true)
    
    
    Action:
    
    Consider defining a bean of type 'sip.sipDemo.repository.mysql.SipRepository' in your configuration.
    
    
    > 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

    import sip.sipDemo.model.mysql.SipEntity;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    @Component
    @Repository
    public interface SipRepository extends JpaRepository&lt;SipEntity,Integer&gt;{
    
    }

SipEntity.java

    package sip.sipDemo.model.mysql;
    
    import javax.persistence.*;
    import java.sql.Date;
    import java.sql.Timestamp;
    
    @Entity
    @Table(name = &quot;sip_table&quot;)
    public class SipEntity {
    
          public SipEntity() {}
         @Id
         @Column(name = &quot;id&quot;)
         @GeneratedValue(strategy = GenerationType.AUTO)
         private int id;
    
         @Column(name = &quot;userid&quot;)
         private int UserId;
    
         @Column(name = &quot;folioNo&quot;)
         private String FolioNo;
    
         @Column(name = &quot;amount&quot;)
         private double Amount;
    
         @Column(name = &quot;isin&quot;)
         private String IsIn;
    
         @Column(name = &quot;schemename&quot;)
         private String SchemeName;
    
         @Column(name = &quot;nextSipDate&quot;)
         private Date NextSipDate;
    
         @Column(name = &quot;status&quot;)
         private String status;
    }
    

SipController.java

    package sip.sipDemo.Controller;
    import sip.sipDemo.Service.SipService;
    import sip.sipDemo.model.mysql.SipEntity;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.ResponseBody;
    import java.util.List;
    import java.util.Optional;
    
    @Controller
    public class SipController {
    
         @Autowired
         private SipService sipservice;
    
    
        @ResponseBody
        @GetMapping(value=&quot;/&quot;)
        public String home()
        {
            return &quot;Hello&quot;;
        }
    
        @ResponseBody
        @GetMapping(value = &quot;/getAllEntities&quot;)
        public List&lt;SipEntity&gt; getAllEntities()
        {
             List&lt;SipEntity&gt; newList = sipservice.getAllEntities();
    
             return newList;
        }
    }

SipService.java

    package sip.sipDemo.Service;
    
    import sip.sipDemo.model.mysql.SipEntity;
    import sip.sipDemo.repository.mysql.SipRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Optional;
    
    @Service
    public class SipService {
    
        @Autowired
        private SipRepository sipRepository;
    
        public List&lt;SipEntity&gt; getAllEntities()
        {
            List&lt;SipEntity&gt; sipList = sipRepository.findAll();
    
            return sipList;
        }
    }
    
    
   

sipDemoApplication.java (main file)

    package sip.sipDemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
    @EnableAutoConfiguration
    @ComponentScan(basePackages = {&quot;sip.sipDemo&quot;})
    @SpringBootApplication
    public class SipDemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SipDemoApplication.class, args);
    		
    	}
    
    }

The Error Message looks like

: APPLICATION FAILED TO START

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

application.properties

    spring.datasource.url= jdbc:mysql://localhost:3306/sys?autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull
    spring.datasource.username=root
    spring.datasource.password=******** (actual password)
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.properties.org.hibernate.envers.audit_table_suffix=_AUDIT_LOG
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    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.
   │&#160;&#160; │&#160;&#160;             └── sipDemo
    │&#160;&#160; │&#160;&#160;                 ├── Controller
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160; └── SipController.java
    │&#160;&#160; │&#160;&#160;                 ├── model
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160; └── mysql
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160;     └── SipEntity.java
    │&#160;&#160; │&#160;&#160;                 ├── repository
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160; └── mysql
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160;     └── SipRepository.java
    │&#160;&#160; │&#160;&#160;                 ├── Service
    │&#160;&#160; │&#160;&#160;                 │&#160;&#160; └── SipService.java
    │&#160;&#160; │&#160;&#160;                 └── SipDemoApplication.java
    │&#160;&#160; └── resources
    │&#160;&#160;     ├── application.properties
    │&#160;&#160;     ├── static
    │&#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:

确定