英文:
Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda
问题
以下是翻译好的内容:
错误:-
{
"errorMessage": "在文件[/var/task/org/qmetech/service/AssociationService.class]中定义的名为'associationService'的bean创建错误: 通过构造函数参数0表达的不满足的依赖项; 嵌套异常是org.springframework.beans.factory.BeanCreationException: 在设置bean属性'mongoOperations'时无法解析对bean'mongoTemplate'的引用; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException: 不存在名为'mongoTemplate'的bean",
"errorType": "org.springframework.beans.factory.UnsatisfiedDependencyException",
"stackTrace": [
该项目有一个根项目,其中有一个子项目'GetAssociationService'
根项目 - 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() }
子项目 - build.gradle
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClassName = 'org.qmetech.GetAssociationService'
}
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
有谁可以告诉我我做错了什么吗?
谢谢!
英文:
I have a spring cloud function project for running the code in AWS Lambda.
The spring boot application runs fine locally. But gives the below error when deployed in 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'
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;
}
}
The Complete Code can be found here - https://github.com/iftekharkhan09/Services
Can Anyone please tell me what am I doing wrong?
Thanks!
答案1
得分: 1
你需要创建 [`MongoTemplate`][1] 的 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`。
[1]: https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html
英文:
You need to create beans of MongoTemplate
since there are none to be injected by default configuration.
@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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论