我正在构建一个基于on this code
的lambda大写服务是“注入”的,如下所示:
@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中。
我尝试了解决方案@ https://www.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas但没有成功。
答案 0 :(得分:3)
您必须先初始化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);
}
}