英文:
Getting UnsatisfiedDependency exception(No qualifying bean of type found) even though bean is defined and using Service annotation
问题
@RestController
@RequestMapping(ProjectRestController.ENDPOINT)
@Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = "项目")
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("获取项目")
@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("创建项目")
@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);
}
}
@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;
}
}
@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;
}
}
@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
Optional<Project> findBySdlcSystemIdAndId(long sdlcSystemId, long projectId);
Optional<Project> findById(long projectId);
}
@Repository
public interface SdlcSystemRepository extends JpaRepository<SdlcSystem, Long> {
Optional<SdlcSystem> findById(long systemId);
}
@Service
public interface ProjectService {
Project getProject(long id);
Project saveProject(Project project);
}
public class ProjectServiceImpl implements ProjectService {
@Autowired
private ProjectRepository projectRepository;
public Project getProject(long id) {
return projectRepository.findById(id).orElseThrow(
() -> new RuntimeException("找不到类" + Project.class + "和 id:" + id));
}
public Project saveProject(Project project) {
try {
return projectRepository.save(project);
} catch (Exception e) {
return null;
}
}
}
@Service
public interface SdlcService {
Optional<SdlcSystem> findById(Long systemId);
}
public class SdlcServiceImpl {
@Autowired
private SdlcSystemRepository sdlcSystemRepository;
SdlcSystem findById(Long systemId) {
return sdlcSystemRepository.findById(systemId).orElseThrow(
() -> new RuntimeException("找不到类" + SdlcSystem.class + "和 id:" + systemId));
}
}
@SpringBootApplication
public class RestapiApplication {
public static void main(String[] args) {
SpringApplication.run(RestapiApplication.class, args);
}
}
英文:
I am trying to run my basic restapi application which has some pojo defined and using service layer to fetch data from h2 database. But when i run the application, i am getting bean not found for one of the service object.
ProjectResController.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
@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
@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> {
Optional<Project> findBySdlcSystemIdAndId(long sdlcSystemId, long projectId);
Optional<Project> findById(long projectId);
}
SdlcsystemRepository.java
@Repository
public interface SdlcSystemRepository extends JpaRepository<SdlcSystem, Long> {
Optional<SdlcSystem> findById(long systemId);
}```
**ProjectService.java**
@Service
public interface 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
@Service
public interface SdlcService {
Optional<SdlcSystem> findById(Long systemId);
}
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. To display the conditions report re-run your application with 'debug' enabled.
2020-09-12 19:35:17.256 ERROR 73258 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
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.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.restapi.restapi.Service.SdlcService' in your configuration.
答案1
得分: 1
你应该用 @Service
注解 SdlcServiceImpl
(而不是它的接口)。
从 @Service
javadoc 中可以看到:
> 表示被注解的 类 是一个 "Service"(…)
>
> 这个注解作为 @Component 的一个特化,允许通过类路径扫描自动检测 实现类。
英文:
You should annotate SdlcServiceImpl
(and not it's interface) with @Service
.
From @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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论