简体   繁体   中英

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. 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] 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM