繁体   English   中英

在springboot AWS Lambda中注入服务

[英]Injecting a service in a springboot aws lambda

我正在基于此代码构建lambda

uppercaseService是这样“注入”的:

@Component("uppercaseFunction")
public class UppercaseFunction implements Function<UppercaseRequest, UppercaseResponse> {

private final UppercaseService uppercaseService;

public UppercaseFunction(final UppercaseService uppercaseService) {
    this.uppercaseService = uppercaseService;
}

在我尝试在UppercaseService中注入另一个服务之前,此方法工作正常。

@Service
public class UppercaseService {

    @Autowired
    MyService myService;

    public String uppercase(final String input) {
        myService.doSomething();
        return input.toUpperCase(Locale.ENGLISH);
    }
}

AWS控制台返回:

“ errorMessage”:“创建名称为'uppercaseService'的bean时出错:通过字段'myService'表示的不满意的依赖性

此服务在非lambda上下文中工作。 该类存在于使用maven软件包构建的.jar中。

我尝试了@@ ://www.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas解决方案,但未成功。

您必须首先初始化MyService bean。 由于您的MyService来自外部服务,因此外部服务很可能与您自己的软件包有不同的软件包

直接:

@SpringBootApplication
public class UpperFunctionApplication {

    @Bean
    public MyService myService() {
       return new MyService(); // You must provide code to construct new MyService bean
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(UpperFunctionApplication.class, args);
    }
}

或通过组件可以:

@SpringBootApplication(scanBasePackageClasses = {UpperFunctionApplication.class, MyService.class})
public class UpperFunctionApplication {

    @Bean
    public MyService myService() {
       return new MyService(); // You must provide code to construct new MyService bean
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(UpperFunctionApplication.class, args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM