代码之家  ›  专栏  ›  技术社区  ›  gyc

在SpringBoot AWS lambda中注入服务

  •  1
  • gyc  · 技术社区  · 8 年前

    我正在建造一个基于lambda的 on this code

    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中。

    我试过解决办法@ https://www.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas 没有成功。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Mạnh Quyết Nguyễn    8 年前

    您必须初始化 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);
        }
    }