Getting UnsatisfiedDependency exception(No qualifying bean of type found) even though bean is defined and using Service annotation

huangapple go评论144阅读模式
英文:

Getting UnsatisfiedDependency exception(No qualifying bean of type found) even though bean is defined and using Service annotation

问题

  1. @RestController
  2. @RequestMapping(ProjectRestController.ENDPOINT)
  3. @Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = "项目")
  4. public class ProjectRestController {
  5. public static final String ENDPOINT = "/api/v2/projects";
  6. public static final String ENDPOINT_ID = "/{id}";
  7. public static final String PATH_VARIABLE_ID = "id";
  8. public static final String ENDPOINT_SYSTEM_ID = "/{systemId}/projects";
  9. private static final String API_PARAM_ID = "ID";
  10. @Autowired
  11. private SdlcService sdlcService;
  12. @Autowired
  13. private ProjectService projectService;
  14. @ApiOperation("获取项目")
  15. @GetMapping(ENDPOINT_ID)
  16. public Project getProject(
  17. @ApiParam(name = API_PARAM_ID, required = true) @PathVariable(PATH_VARIABLE_ID) final long projectId) {
  18. return projectService.getProject(projectId);
  19. }
  20. @ApiOperation("创建项目")
  21. @PostMapping(ENDPOINT_SYSTEM_ID)
  22. public Project createProject(@PathVariable(value = "systemId") Long systemId, @Valid @RequestBody Project project) {
  23. sdlcService.findById(systemId).ifPresent(project::setSdlcSystem);
  24. return projectService.saveProject(project);
  25. }
  26. }
  1. @Entity
  2. @lombok.Data
  3. @Table(name = "project")
  4. @EntityListeners(AuditingEntityListener.class)
  5. public class Project {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private long id;
  9. public long getId() {
  10. return this.id;
  11. }
  12. public void setId(long id) {
  13. this.id = id;
  14. }
  15. @Column(name = "external_id", nullable = false)
  16. @NotBlank
  17. private String externalId;
  18. public String getExternalId() {
  19. return this.externalId;
  20. }
  21. public void setExternalId(String externalId) {
  22. this.externalId = externalId;
  23. }
  24. @Column(name = "name")
  25. private String name;
  26. public String getName() {
  27. return this.name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. @ManyToOne
  33. @JoinColumn(name = "sdlc_system_id")
  34. @NotNull
  35. private SdlcSystem sdlcSystem;
  36. public SdlcSystem getSdlcSystem() {
  37. return this.sdlcSystem;
  38. }
  39. public void setSdlcSystem(SdlcSystem sdlcSystem) {
  40. this.sdlcSystem = sdlcSystem;
  41. }
  42. @Column(name = "created_date", nullable = false)
  43. @CreatedDate
  44. private Instant createdDate;
  45. public Instant getCreatedDate() {
  46. return this.createdDate;
  47. }
  48. public void setCreatedDate(Instant createdDate) {
  49. this.createdDate = createdDate;
  50. }
  51. @Column(name = "last_modified_date", nullable = false)
  52. @LastModifiedDate
  53. private Instant lastModifiedDate;
  54. public Instant getLastModifiedDate() {
  55. return this.lastModifiedDate;
  56. }
  57. public void setLastModifiedDate(Instant lastModifiedDate) {
  58. this.lastModifiedDate = lastModifiedDate;
  59. }
  60. }
  1. @Data
  2. @Entity
  3. @Table(name = "sdlc_system")
  4. @EntityListeners(AuditingEntityListener.class)
  5. public class SdlcSystem {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private long id;
  9. @NotEmpty
  10. @URL
  11. @Column(name = "base_url", nullable = false)
  12. private String baseUrl;
  13. public String getBaseUrl() {
  14. return this.baseUrl;
  15. }
  16. public void setBaseUrl(String baseUrl) {
  17. this.baseUrl = baseUrl;
  18. }
  19. @Column(name = "description")
  20. private String description;
  21. public String getDescription() {
  22. return this.description;
  23. }
  24. public void setDescription(String description) {
  25. this.description = description;
  26. }
  27. @Column(name = "created_date", nullable = false)
  28. @CreatedDate
  29. private Instant createdDate;
  30. public Instant getCreatedDate() {
  31. return this.createdDate;
  32. }
  33. public void setCreatedDate(Instant createdDate) {
  34. this.createdDate = createdDate;
  35. }
  36. @Column(name = "last_modified_date", nullable = false)
  37. @LastModifiedDate
  38. private Instant lastModifiedDate;
  39. public Instant getLastModifiedDate() {
  40. return this.lastModifiedDate;
  41. }
  42. public void setLastModifiedDate(Instant lastModifiedDate) {
  43. this.lastModifiedDate = lastModifiedDate;
  44. }
  45. }
  1. @Repository
  2. public interface ProjectRepository extends JpaRepository<Project, Long> {
  3. Optional<Project> findBySdlcSystemIdAndId(long sdlcSystemId, long projectId);
  4. Optional<Project> findById(long projectId);
  5. }
  1. @Repository
  2. public interface SdlcSystemRepository extends JpaRepository<SdlcSystem, Long> {
  3. Optional<SdlcSystem> findById(long systemId);
  4. }
  1. @Service
  2. public interface ProjectService {
  3. Project getProject(long id);
  4. Project saveProject(Project project);
  5. }
  1. public class ProjectServiceImpl implements ProjectService {
  2. @Autowired
  3. private ProjectRepository projectRepository;
  4. public Project getProject(long id) {
  5. return projectRepository.findById(id).orElseThrow(
  6. () -> new RuntimeException("找不到类" + Project.class + "和 id:" + id));
  7. }
  8. public Project saveProject(Project project) {
  9. try {
  10. return projectRepository.save(project);
  11. } catch (Exception e) {
  12. return null;
  13. }
  14. }
  15. }
  1. @Service
  2. public interface SdlcService {
  3. Optional<SdlcSystem> findById(Long systemId);
  4. }
  1. public class SdlcServiceImpl {
  2. @Autowired
  3. private SdlcSystemRepository sdlcSystemRepository;
  4. SdlcSystem findById(Long systemId) {
  5. return sdlcSystemRepository.findById(systemId).orElseThrow(
  6. () -> new RuntimeException("找不到类" + SdlcSystem.class + "和 id:" + systemId));
  7. }
  8. }
  1. @SpringBootApplication
  2. public class RestapiApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(RestapiApplication.class, args);
  5. }
  6. }
英文:

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

  1. @RestController
  2. @RequestMapping(ProjectRestController.ENDPOINT)
  3. @Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = &quot;Project&quot;)
  4. public class ProjectRestController {
  5. public static final String ENDPOINT = &quot;/api/v2/projects&quot;;
  6. public static final String ENDPOINT_ID = &quot;/{id}&quot;;
  7. public static final String PATH_VARIABLE_ID = &quot;id&quot;;
  8. public static final String ENDPOINT_SYSTEM_ID = &quot;/{systemId}/projects&quot;;
  9. private static final String API_PARAM_ID = &quot;ID&quot;;
  10. @Autowired
  11. private SdlcService sdlcService;
  12. @Autowired
  13. private ProjectService projectService;
  14. @ApiOperation(&quot;Get a Project&quot;)
  15. @GetMapping(ENDPOINT_ID)
  16. public Project getProject(
  17. @ApiParam(name = API_PARAM_ID, required = true) @PathVariable(PATH_VARIABLE_ID) final long projectId) {
  18. return projectService.getProject(projectId);
  19. }
  20. @ApiOperation(&quot;Create a Project&quot;)
  21. @PostMapping(ENDPOINT_SYSTEM_ID)
  22. public Project createProject(@PathVariable(value = &quot;systemId&quot;) Long systemId, @Valid @RequestBody Project project) {
  23. sdlcService.findById(systemId).ifPresent(project::setSdlcSystem);
  24. return projectService.saveProject(project);
  25. }
  26. }

Project.java

  1. @Entity
  2. @lombok.Data
  3. @Table(name = &quot;project&quot;)
  4. @EntityListeners(AuditingEntityListener.class)
  5. public class Project {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private long id;
  9. public long getId() {
  10. return this.id;
  11. }
  12. public void setId(long id) {
  13. this.id = id;
  14. }
  15. @Column(name = &quot;external_id&quot;, nullable = false)
  16. @NotBlank
  17. private String externalId;
  18. public String getExternalId() {
  19. return this.externalId;
  20. }
  21. public void setExternalId(String externalId) {
  22. this.externalId = externalId;
  23. }
  24. @Column(name = &quot;name&quot;)
  25. private String name;
  26. public String getName() {
  27. return this.name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. @ManyToOne
  33. @JoinColumn(name = &quot;sdlc_system_id&quot;)
  34. @NotNull
  35. private SdlcSystem sdlcSystem;
  36. public SdlcSystem getSdlcSystem() {
  37. return this.sdlcSystem;
  38. }
  39. public void setSdlcSystem(SdlcSystem sdlcSystem) {
  40. this.sdlcSystem = sdlcSystem;
  41. }
  42. @Column(name = &quot;created_date&quot;, nullable = false)
  43. @CreatedDate
  44. private Instant createdDate;
  45. public Instant getCreatedDate() {
  46. return this.createdDate;
  47. }
  48. public void setCreatedDate(Instant createdDate) {
  49. this.createdDate = createdDate;
  50. }
  51. @Column(name = &quot;last_modified_date&quot;, nullable = false)
  52. @LastModifiedDate
  53. private Instant lastModifiedDate;
  54. public Instant getLastModifiedDate() {
  55. return this.lastModifiedDate;
  56. }
  57. public void setLastModifiedDate(Instant lastModifiedDate) {
  58. this.lastModifiedDate = lastModifiedDate;
  59. }
  60. }

SdlcSystem.java

  1. @Data
  2. @Entity
  3. @Table(name = &quot;sdlc_system&quot;)
  4. @EntityListeners(AuditingEntityListener.class)
  5. public class SdlcSystem {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private long id;
  9. @NotEmpty
  10. @URL
  11. @Column(name = &quot;base_url&quot;, nullable = false)
  12. private String baseUrl;
  13. public String getBaseUrl() {
  14. return this.baseUrl;
  15. }
  16. public void setBaseUrl(String baseUrl) {
  17. this.baseUrl = baseUrl;
  18. }
  19. @Column(name = &quot;description&quot;)
  20. private String description;
  21. public String getDescription() {
  22. return this.description;
  23. }
  24. public void setDescription(String description) {
  25. this.description = description;
  26. }
  27. @Column(name = &quot;created_date&quot;, nullable = false)
  28. @CreatedDate
  29. private Instant createdDate;
  30. public Instant getCreatedDate() {
  31. return this.createdDate;
  32. }
  33. public void setCreatedDate(Instant createdDate) {
  34. this.createdDate = createdDate;
  35. }
  36. @Column(name = &quot;last_modified_date&quot;, nullable = false)
  37. @LastModifiedDate
  38. private Instant lastModifiedDate;
  39. public Instant getLastModifiedDate() {
  40. return this.lastModifiedDate;
  41. }
  42. public void setLastModifiedDate(Instant lastModifiedDate) {
  43. this.lastModifiedDate = lastModifiedDate;
  44. }
  45. }

ProjectRepository.java

  1. @Repository
  2. public interface ProjectRepository extends JpaRepository&lt;Project, Long&gt; {
  3. Optional&lt;Project&gt; findBySdlcSystemIdAndId(long sdlcSystemId, long projectId);
  4. Optional&lt;Project&gt; findById(long projectId);
  5. }

SdlcsystemRepository.java

  1. @Repository
  2. public interface SdlcSystemRepository extends JpaRepository&lt;SdlcSystem, Long&gt; {
  3. Optional&lt;SdlcSystem&gt; findById(long systemId);
  4. }```
  5. **ProjectService.java**
  1. @Service

public interface ProjectService {

  1. Project getProject(long id);
  2. Project saveProject(Project project);

}

  1. **ProjectServiceImpl.java**
  2. ``` public class ProjectServiceImpl implements ProjectService {
  3. @Autowired
  4. private ProjectRepository projectRepository;
  5. public Project getProject(long id) {
  6. return projectRepository.findById(id).orElseThrow(
  7. () -&gt; new RuntimeException(&quot;Value Not found for class&quot; + Project.class + &quot; and id: &quot; + id));
  8. }
  9. public Project saveProject(Project project) {
  10. try {
  11. return projectRepository.save(project);
  12. } catch (Exception e) {
  13. return null;
  14. }
  15. }
  16. }

SdlcService.java

  1. @Service
  2. public interface SdlcService {
  3. Optional&lt;SdlcSystem&gt; findById(Long systemId);
  4. }

SdlcServiceImpl.java

  1. public class SdlcServiceImpl {
  2. @Autowired
  3. private SdlcSystemRepository sdlcSystemRepository;
  4. SdlcSystem findById(Long systemId) {
  5. return sdlcSystemRepository.findById(systemId).orElseThrow(
  6. () -&gt; new RuntimeException(&quot;Value Not found for class&quot; + SdlcSystem.class + &quot; and id: &quot; + systemId));
  7. }
  8. }

Restapiapplication.java

  1. @SpringBootApplication
  2. public class RestapiApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(RestapiApplication.class, args);
  5. }
  6. }

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.

huangapple
  • 本文由 发表于 2020年9月12日 22:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63861297.html
匿名

发表评论

匿名网友

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

确定