我正在构建一个基于 lambda 的 on this code
uppercaseService 是这样“注入(inject)”的:
@Component("uppercaseFunction")
public class UppercaseFunction implements Function<UppercaseRequest, UppercaseResponse> {
private final UppercaseService uppercaseService;
public UppercaseFunction(final UppercaseService uppercaseService) {
this.uppercaseService = uppercaseService;
}
在我尝试在 UppercaseService 中注入(inject)另一个服务之前,一切正常。
@Service
public class UppercaseService {
@Autowired
MyService myService;
public String uppercase(final String input) {
myService.doSomething();
return input.toUpperCase(Locale.ENGLISH);
}
}
AWS 控制台返回:
"errorMessage": "Error creating bean with name 'uppercaseService': Unsatisfied dependency expressed through field 'myService'
此服务在非 lambda 上下文中工作。该类存在于使用 maven 包构建的 .jar 中。
我尝试了@ https://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);
}
}
关于java - 在 springboot aws lambda 中注入(inject)服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50449673/