简体   繁体   English

在部署 AWS Lambda 时设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用

[英]Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda

I have a spring cloud function project for running the code in AWS Lambda.我有一个用于在 AWS Lambda 中运行代码的 spring 云函数项目。 The spring boot application runs fine locally. spring boot 应用程序在本地运行良好。 But gives the below error when deployed in AWS Lambda.但是在 AWS Lambda 中部署时会出现以下错误。

Error:-错误:-

{
  "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": [

The project has a root project which has a subproject 'GetAssociationService'该项目有一个根项目,其中有一个子项目“GetAssociationService”

rootproject - build.gradle根项目 - 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 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用户存储库.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;
    }
}

The Complete Code can be found here - https://github.com/iftekharkhan09/Services完整代码可以在这里找到 - https://github.com/iftekharkhan09/Services

Can Anyone please tell me what am I doing wrong?谁能告诉我我做错了什么?

Thanks!谢谢!

You need to create beans of MongoTemplate since there are none to be injected by default configuration.您需要创建MongoTemplate bean,因为默认配置没有要注入的 bean。

@Bean
public MongoClient mongoClient() {
    return new MongoClient("localhost", 27017);
}

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

Also, don't forget to use com.mongodb.client.MongoClient which replaces reprecated com.mongodb.MongoClient as of Spring Boot 2.2.另外,不要忘记使用com.mongodb.client.MongoClient从 Spring Boot 2.2 开始替换已推荐的com.mongodb.MongoClient

暂无
暂无

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

相关问题 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' Spring 引导 + Kotlin + MongoDb:设置 bean 属性时无法解析对 bean 'mongoTemplate' 的引用 - Spring boot + Kotlin + MongoDb: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用 - Cannot resolve reference to bean while setting bean property 'userDetailsService' 创建名称为“ mongoTransactionManager”的bean时出错:设置bean属性“ datastore”时无法解析对bean“ mongoDatastore”的引用 - Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore' 设置bean属性&#39;dataSource&#39;时,无法解析对bean&#39;DataSource&#39;的引用.factory.NoSuchBeanDefinitionException: - Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource' .factory.NoSuchBeanDefinitionException: 使用键[0]设置bean属性“触发”时,无法解析对bean“ cronTrigger”的引用 - Cannot resolve reference to bean 'cronTrigger' while setting bean property 'triggers' with key [0] 使用键[0]设置bean属性“ sourceList”时,无法解析对bean&#39;org.springframework.security.web.DefaultSecurityFilterChain#0&#39;的引用 - cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0] 创建名称为&#39;org.springframework.security.filterChains的bean时出错,设置bean属性&#39;sourceList&#39;时无法解析对bean的引用 - Error creating bean with name 'org.springframework.security.filterChains Cannot resolve reference to bean while setting bean property 'sourceList' Spring Security:设置构造函数参数时无法解析对bean的引用 - Spring security : Cannot resolve reference to bean while setting constructor argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM