自动装配的依赖项失败

时间:2019-05-20 05:47:43

标签: java spring

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 );

    }

}

控制器类

@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;


}

控制器类

@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;
    }
  

创建名称为'partnershipMaintenanceController'的bean时出错:   自动连接的依赖项注入失败;嵌套异常为   org.springframework.beans.factory.BeanCreationException:无法   自动连线栏位:   com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService   com.cgi.bkifs.rest.service.controller.PartnershipMaintenanceController.partnershipService;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型的合格豆   [com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService]   找到依赖关系:至少需要1个符合条件的bean   自动关联此依赖项的候选对象。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(required = true)}

3 个答案:

答案 0 :(得分:1)

可能您忘记了将PartnershipService用作Spring Bean。

   @Component
   public class PartnershipService{}

答案 1 :(得分:1)

如果bean(PartnershipService)是普通bean,则可以使用@Component
如果bean(PartnershipService)是服务bean(服务层),则可以使用@service

  

有关@ Component,@ Service,@ Controller和@Repository的信息   在Spring Framework中做注解:
@Component是通用构造型   用于任何Spring管理的组件或bean。
@Repository是一个刻板印象   对于持久层。
@Service是服务的构造型   层。
@Controller是表示层的构造型   (spring-MVC)。

答案 2 :(得分:0)

错误消息显示:

  

无法自动连线字段:   com.cgi.bkifs.bso.prime.partnership.maintainence.service.PartnershipService

假设您已经声明了一个类PartnershipService并具有@Service注释,则可能您未定义组件扫描。

要么添加

@ComponentScan(basePackages = "com.cgi.bkifs.bso.prime.partnership")或更早的版本中,使用xml文件定义组件扫描的范围:

<?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>