自动连线为null(基本示例)

时间:2019-03-25 12:38:15

标签: java spring

我的服务:

@Service
public class MyService {

    @Autowired
    private RequestMapper requestMapper;

  ... 

  // Somewhere I use the mapper...
  request = requestMapper.applicationToBasicRequest(application.get());

}

我的界面

@Mapper(componentModel = "spring", uses = { BankAccountMapper.class, ApplicantMapper.class, ObligationsMapper.class })
public interface RequestMapper {

 ....
}

使用运行方式-> Java应用程序启动应用程序时,出现以下错误:

  

project.service.MyService中的字段requestMapper需要一个类型为bean的bean   无法执行的“ project.RequestMapper”   被发现。

     

注入点具有以下注释:     -@ org.springframework.beans.factory.annotation.Autowired(required = true)

如果我不能在界面中放置@Autowired,该如何初始化?

1 个答案:

答案 0 :(得分:0)

制作RequestMapper接口的实现类,即用@Service注释

@Service
public class RequestMapperImpl implements RequestMapper{

 @Overried
 public Result applicationToBasicRequest(Object obj){
  // Do your work here
  return null;
 }

}

在任何控制器中自动连接RequestMapper时,spring IOC容器会自动将RequestMapperImpl对象注入RequestMapper实例。

或者您可以为您的特定依赖项注入提供@Qualifier批注。当您具有单个接口的多个实现类时需要它