英文:
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
可能的错误来源是:
- 检查您的包扫描路径是否正确。根据您提供的目录结构,您的主应用程序类(SipDemoApplication.java)位于
sip.sipDemo
包中。确保包扫描路径设置正确,以便Spring能够找到您的组件和存储库。 - 确保您的存储库接口(SipRepository.java)位于与主应用程序类相同的包或其子包中。Spring Data JPA需要正确扫描这些接口。
- 确保您的数据库配置正确。检查
application.properties
文件中的数据库连接信息,包括数据库URL、用户名和密码。 - 确保您的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<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 (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 = {"sip.sipDemo"})
@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 '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
application.properties
spring.datasource.url= jdbc:mysql://localhost:3306/sys?autoReconnect=true&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
- I have used mysql database.
- The main class is in the outer most layer and the packages are well structured.
│   │   └── sipDemo
│   │   ├── Controller
│   │   │   └── SipController.java
│   │   ├── model
│   │   │   └── mysql
│   │   │   └── SipEntity.java
│   │   ├── repository
│   │   │   └── mysql
│   │   │   └── SipRepository.java
│   │   ├── Service
│   │   │   └── SipService.java
│   │   └── SipDemoApplication.java
│   └── resources
│   ├── application.properties
│   ├── static
│   └── templates
- If I remove @Autowire for the sipRepository object in sipService, the controller endpoints for sipRepository do not work but the rest is working fine.
- Please provide the possible source of error.
专注分享java语言的经验与见解,让所有开发者获益!
评论