在没有spring的java项目中使用服务层功能

问题描述

我正在用 java 编写一个 azure 函数。为了使用良好的模块化模式,我创建了一个具有抽象接口类的服务层和一个实现服务层的 impl 层。

但是,我没有使用 Spring-framework,因此我无法使用 @Autowired 在运行程序文件中创建服务层的单例实例。如何在我的跑步者类(或我项目中的其他地方)中使用我的服务层函数

服务层

public interface TimeTriggeredService {

  String getLogs(String token,String url,final ExecutionContext context);
}

Impl 层

public class TimeTriggeredServiceImpl implements TimeTriggeredService {

  public String getLogs(String token,final ExecutionContext context) {
    // Some logic
  }
}

跑步类

public class TimeTriggeredFunction {

  @FunctionName("TimeTriggeredFunction")
  public void run(@TimerTrigger(name = "timerInfo",schedule = "0 */1 * * * *") String timerInfo,final ExecutionContext context) {
    
    String timeAuditLogs = TimeTriggeredService.getLogs(token,URL,context);  // unsure what should replace this line or what should be done before this. 
  }
}

注意:这不是春季项目。

解决方法

尚不支持 Java 的依赖注入。请查看 GitHub 问题:#324

目前,您可以使用 Spring Framework 来使用 Azure Function for HTTP 仅请求(不是绑定。

Here 是如何使用它的示例。

如果不想使用 Spring 框架,则必须在类中创建一个全局实例并使用它。