Tushar Sharma
Tushar Sharma

Reputation: 19

Dependency of autowire failes

public class PartnershipMaintenanceFunction
{
    final Logger logger = LoggerFactory.getLogger( PartnershipMaintenanceFunction.class );

    @Autowired
    PartnershipMaintenanceController partnershipMaintenanceServiceController;

    public RetrievePartnershipResponse retrievePartnership( Message<RetrievePartnershipRequest> messageRequest )
    {
        RetrievePartnershipRequest retrievePartnershipRequest = messageRequest.getPayload();

        MessageHeaders header = messageRequest.getHeaders();

        return partnershipMaintenanceServiceController.retrievePartnership( retrievePartnershipRequest );

    }

}

controller class

@RestController
@Api( "Partnership Maintainence" )
public class PartnershipMaintenanceController
{
    final Logger logger = LoggerFactory.getLogger( PartnershipMaintenanceController.class );

    @Autowired
    PartnershipService partnershipService;

    public void setPartnershipService( PartnershipService partnershipService )
    {
        this.partnershipService = partnershipService;
    }


    @GET
    @Path( "/retrievePartnershipRequest" )
    @ApiOperation( "Retrieve Partnership" )
    public RetrievePartnershipResponse retrievePartnership( RetrievePartnershipRequest request )
    {
        return partnershipService.retrievePartnership( request );

    }
}



public class PartnershipMaintenanceFunction
{
    final Logger logger = LoggerFactory.getLogger( PartnershipMaintenanceFunction.class );

    @Autowired
    PartnershipMaintenanceController partnershipMaintenanceServiceController;


}

controller class

@RestController
@Api( "Partnership Maintainence" )
public class PartnershipMaintenanceController
{
    final Logger logger = LoggerFactory.getLogger( PartnershipMaintenanceController.class );

    @Autowired
    PartnershipService partnershipService;

    public void setPartnershipService( PartnershipService partnershipService )
    {
        this.partnershipService = partnershipService;
    }

Error creating bean with name 'partnershipMaintenanceController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService com.cgi.bkifs.rest.service.controller.PartnershipMaintenanceController.partnershipService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Upvotes: 1

Views: 55

Answers (3)

Lova Chittumuri
Lova Chittumuri

Reputation: 3303

If the bean ( PartnershipService ) is Normal bean then you can use @Component
If the bean (PartnershipService ) is service bean ( Service layer ) then you can use @service

Information about @Component, @Service, @Controller, and @Repository annotation do in Spring Framework:
@Component is a generic stereotype for any Spring-managed component or bean.
@Repository is a stereotype for the persistence layer.
@Service is a stereotype for the service layer.
@Controller is a stereotype for the presentation layer (spring-MVC).

Upvotes: 1

stacker
stacker

Reputation: 68962

The error messages says:

Could not autowire field: com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService

Assuming that you already declared a class PartnershipService and it has the @Service annotation it is likely that you did not define the component scan.

Either add

@ComponentScan(basePackages = "com.cgi.bkifs.bso.prime.partnership") or in older versions use the xml file to define the scope of component scan:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <context:component-scan base-package="com.cgi.bkifs.bso.prime.partnership"/>

</beans>

Upvotes: 0

Dilip Chauhan
Dilip Chauhan

Reputation: 177

Probably you forgot to making PartnershipService as Spring Bean.

   @Component
   public class PartnershipService{}

Upvotes: 1

Related Questions