【问题标题】:Using service layer functions in a java project without spring在没有spring的java项目中使用服务层函数
【发布时间】:2021-07-11 07:48:39
【问题描述】:

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

但是,我没有使用Spring-framework,所以我不能使用@Autowired 在运行器文件中创建服务层的单例实例。如何在我的 runner 类(或我项目中的其他地方)中使用我的服务层函数?

服务层

public interface TimeTriggeredService {

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

实现层

public class TimeTriggeredServiceImpl implements TimeTriggeredService {

  public String getLogs(String token, String url,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. 
  }
}

注意:这不是春季项目。

【问题讨论】:

  • @Inject 会为你做 DI。
  • 下面的答案有帮助吗?

标签: java azure-functions singleton service-layer


【解决方案1】:

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

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

Here 是如何使用它的示例。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2021-03-02
    相关资源
    最近更新 更多