[英]Getting UnsatisfiedDependency exception(No qualifying bean of type found) even though bean is defined and using Service annotation
I am trying to run my basic restapi application which has some pojo defined and using service layer to fetch data from h2 database.我正在尝试运行我的基本 restapi 应用程序,它定义了一些 pojo 并使用服务层从 h2 数据库中获取数据。 But when i run the application, i am getting bean not found for one of the service object.但是,当我运行该应用程序时,我找不到服务对象之一的 bean。
ProjectResController.java项目资源控制器.java
@RestController
@RequestMapping(ProjectRestController.ENDPOINT)
@Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = "Project")
public class ProjectRestController {
public static final String ENDPOINT = "/api/v2/projects";
public static final String ENDPOINT_ID = "/{id}";
public static final String PATH_VARIABLE_ID = "id";
public static final String ENDPOINT_SYSTEM_ID = "/{systemId}/projects";
private static final String API_PARAM_ID = "ID";
@Autowired
private SdlcService sdlcService;
@Autowired
private ProjectService projectService;
@ApiOperation("Get a Project")
@GetMapping(ENDPOINT_ID)
public Project getProject(
@ApiParam(name = API_PARAM_ID, required = true) @PathVariable(PATH_VARIABLE_ID) final long projectId) {
return projectService.getProject(projectId);
}
@ApiOperation("Create a Project")
@PostMapping(ENDPOINT_SYSTEM_ID)
public Project createProject(@PathVariable(value = "systemId") Long systemId, @Valid @RequestBody Project project) {
sdlcService.findById(systemId).ifPresent(project::setSdlcSystem);
return projectService.saveProject(project);
}
}
Project.java项目.java
@Entity
@lombok.Data
@Table(name = "project")
@EntityListeners(AuditingEntityListener.class)
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "external_id", nullable = false)
@NotBlank
private String externalId;
public String getExternalId() {
return this.externalId;
}
public void setExternalId(String externalId) {
this.externalId = externalId;
}
@Column(name = "name")
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name = "sdlc_system_id")
@NotNull
private SdlcSystem sdlcSystem;
public SdlcSystem getSdlcSystem() {
return this.sdlcSystem;
}
public void setSdlcSystem(SdlcSystem sdlcSystem) {
this.sdlcSystem = sdlcSystem;
}
@Column(name = "created_date", nullable = false)
@CreatedDate
private Instant createdDate;
public Instant getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}
@Column(name = "last_modified_date", nullable = false)
@LastModifiedDate
private Instant lastModifiedDate;
public Instant getLastModifiedDate() {
return this.lastModifiedDate;
}
public void setLastModifiedDate(Instant lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
SdlcSystem.java系统文件
@Data
@Entity
@Table(name = "sdlc_system")
@EntityListeners(AuditingEntityListener.class)
public class SdlcSystem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotEmpty
@URL
@Column(name = "base_url", nullable = false)
private String baseUrl;
public String getBaseUrl() {
return this.baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
@Column(name = "description")
private String description;
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "created_date", nullable = false)
@CreatedDate
private Instant createdDate;
public Instant getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}
@Column(name = "last_modified_date", nullable = false)
@LastModifiedDate
private Instant lastModifiedDate;
public Instant getLastModifiedDate() {
return this.lastModifiedDate;
}
public void setLastModifiedDate(Instant lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
ProjectRepository.java项目库.java
@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
Optional<Project> findBySdlcSystemIdAndId(long sdlcSystemId, long projectId);
Optional<Project> findById(long projectId);
}
SdlcsystemRepository.java SdlcsystemRepository.java
@Repository
public interface SdlcSystemRepository extends JpaRepository<SdlcSystem, Long> {
Optional<SdlcSystem> findById(long systemId);
}```
**ProjectService.java**
@Service
public interface ProjectService {公共接口 ProjectService {
Project getProject(long id);
Project saveProject(Project project);
} }
**ProjectServiceImpl.java**
``` public class ProjectServiceImpl implements ProjectService {
@Autowired
private ProjectRepository projectRepository;
public Project getProject(long id) {
return projectRepository.findById(id).orElseThrow(
() -> new RuntimeException("Value Not found for class" + Project.class + " and id: " + id));
}
public Project saveProject(Project project) {
try {
return projectRepository.save(project);
} catch (Exception e) {
return null;
}
}
}
SdlcService.java SdlcService.java
@Service
public interface SdlcService {
Optional<SdlcSystem> findById(Long systemId);
}
SdlcServiceImpl.java SdlcServiceImpl.java
public class SdlcServiceImpl {
@Autowired
private SdlcSystemRepository sdlcSystemRepository;
SdlcSystem findById(Long systemId) {
return sdlcSystemRepository.findById(systemId).orElseThrow(
() -> new RuntimeException("Value Not found for class" + SdlcSystem.class + " and id: " + systemId));
}
}
Restapiapplication.java应用程序
@SpringBootApplication
public class RestapiApplication {
public static void main(String[] args) {
SpringApplication.run(RestapiApplication.class, args);
}
}
Error错误
Error starting ApplicationContext.启动 ApplicationContext 时出错。 To display the conditions report re-run your application with 'debug' enabled.要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2020-09-12 19:35:17.256 ERROR 73258 --- [ main] osbdLoggingFailureAnalysisReporter : 2020-09-12 19:35:17.256 错误 73258 --- [主要] osbdLoggingFailureAnalysisReporter:
APPLICATION FAILED TO START应用程序无法启动
Description:描述:
Field sdlcService in com.restapi.restapi.Controller.ProjectRestController required a bean of type 'com.restapi.restapi.Service.SdlcService' that could not be found. com.restapi.restapi.Controller.ProjectRestController 中的字段 sdlcService 需要一个无法找到的类型为“com.restapi.restapi.Service.SdlcService”的 bean。
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:行动:
Consider defining a bean of type 'com.restapi.restapi.Service.SdlcService' in your configuration.考虑在您的配置中定义一个“com.restapi.restapi.Service.SdlcService”类型的 bean。
You should annotate SdlcServiceImpl
(and not it's interface) with @Service
.你应该注释SdlcServiceImpl
(而不是它的接口)与@Service
。
From @Service
javadoc :来自@Service
javadoc :
Indicates that an annotated class is a "Service" (...)表示带注释的类是“服务”(...)
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.此注释作为@Component的专业化,允许通过使用classpath扫描来自动检测实现类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.