【问题标题】:Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda在部署 AWS Lambda 时设置 bean 属性 'mongoOperations' 时无法解析对 bean 'mongoTemplate' 的引用
【发布时间】:2020-06-26 18:58:53
【问题描述】:

我有一个 spring 云函数项目,用于在 AWS Lambda 中运行代码。 Spring Boot 应用程序在本地运行良好。但是在 AWS Lambda 中部署时会出现以下错误。

错误:-

{
  "errorMessage": "Error creating bean with name 'associationService' defined in file [/var/task/org/qmetech/service/AssociationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available",
  "errorType": "org.springframework.beans.factory.UnsatisfiedDependencyException",
  "stackTrace": [

该项目有一个根项目,该项目有一个子项目“GetAssociationService”

rootproject - build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

bootJar { enabled = false }

jar { enabled = true }

subprojects {
    group = 'org.qmetech'
    version = '1.0.0'
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'com.github.johnrengelman.shadow'
    apply plugin: 'java'
}

repositories { mavenCentral ( ) }

ext { set ( 'springCloudVersion' , "Hoxton.SR3" ) }

dependencies {
    testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
        exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
    }
}

ext.libraries = [
        commonlibraries: ['org.springframework.boot:spring-boot-starter:2.2.5.RELEASE' ,
                          'org.springframework.cloud:spring-cloud-function-context' ,
                          'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE' ,
                          'com.amazonaws:aws-lambda-java-log4j:1.0.0' ,
                          'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE' ,
                          'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
                          'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE' ,
                          'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
                          'org.springframework.data:spring-data-mongodb:2.2.5.RELEASE'
        ] ,
]

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test { useJUnitPlatform ( ) }

ChildProject - build.gradle

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

springBoot {
    mainClassName = 'org.qmetech.GetAssociationService'
}

dependencies {
    dependencies {
        compile libraries.commonlibraries
        implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    }
}

UserRepository.java

package org.qmetech.repository;

import org.qmetech.domain.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends MongoRepository<User, Integer> { }

AssociationService.java

package org.qmetech.service;

import org.qmetech.domain.User;
import org.qmetech.repository.UserRepository;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;

@Component
public class AssociationService implements Function<String, List<User>> {

    private final UserRepository userRepository;

    public AssociationService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public List<User> apply(String uppercaseRequest) {
        List<User> users = userRepository.findAll();
        return users;
    }
}

完整的代码可以在这里找到 - https://github.com/iftekharkhan09/Services

谁能告诉我我做错了什么?

谢谢!

【问题讨论】:

    标签: java spring spring-boot gradle aws-lambda


    【解决方案1】:

    您需要创建 MongoTemplate 的 bean,因为默认配置没有注入。

    @Bean
    public MongoClient mongoClient() {
        return new MongoClient("localhost", 27017);
    }
    
    @Bean
    public MongoTemplate mongoTemplate(MongoClient mongoClient) throws Exception {
        return new MongoTemplate(mongoClient, "databaseName");
    }
    

    另外,不要忘记使用 com.mongodb.client.MongoClient 替换 Spring Boot 2.2 中的 com.mongodb.MongoClient

    【讨论】:

    • 如果这是导致问题的原因,那么它是如何在本地工作的?
    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 2021-03-10
    • 2017-08-17
    • 2018-04-29
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    相关资源
    最近更新 更多