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

huangapple go评论125阅读模式
英文:

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

问题

以下是翻译好的内容:

错误:-

  1. {
  2. "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",
  3. "errorType": "org.springframework.beans.factory.UnsatisfiedDependencyException",
  4. "stackTrace": [

该项目有一个根项目,其中有一个子项目'GetAssociationService'

根项目 - build.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.2.5.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  4. id 'java'
  5. id 'com.github.johnrengelman.shadow' version '5.2.0'
  6. }
  7. bootJar { enabled = false }
  8. jar { enabled = true }
  9. subprojects {
  10. group = 'org.qmetech'
  11. version = '1.0.0'
  12. sourceCompatibility = '1.8'
  13. targetCompatibility = '1.8'
  14. apply plugin: 'org.springframework.boot'
  15. apply plugin: 'io.spring.dependency-management'
  16. apply plugin: 'com.github.johnrengelman.shadow'
  17. apply plugin: 'java'
  18. }
  19. repositories { mavenCentral() }
  20. ext { set 'springCloudVersion', "Hoxton.SR3" }
  21. dependencies {
  22. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  23. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  24. }
  25. }
  26. ext.libraries = [
  27. commonlibraries: ['org.springframework.boot:spring-boot-starter:2.2.5.RELEASE',
  28. 'org.springframework.cloud:spring-cloud-function-context',
  29. 'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE',
  30. 'com.amazonaws:aws-lambda-java-log4j:1.0.0',
  31. 'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE',
  32. 'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE',
  33. 'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE',
  34. 'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE',
  35. 'org.springframework.data:spring-data-mongodb:2.2.5.RELEASE'
  36. ],
  37. ]
  38. dependencyManagement {
  39. imports {
  40. mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  41. }
  42. }
  43. test { useJUnitPlatform() }

子项目 - build.gradle

  1. sourceCompatibility = 1.8
  2. targetCompatibility = 1.8
  3. repositories {
  4. mavenCentral()
  5. }
  6. springBoot {
  7. mainClassName = 'org.qmetech.GetAssociationService'
  8. }
  9. dependencies {
  10. compile libraries.commonlibraries
  11. implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
  12. }

UserRepository.java

  1. package org.qmetech.repository;
  2. import org.qmetech.domain.User;
  3. import org.springframework.data.mongodb.repository.MongoRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface UserRepository extends MongoRepository<User, Integer> { }

AssociationService.java

  1. package org.qmetech.service;
  2. import org.qmetech.domain.User;
  3. import org.qmetech.repository.UserRepository;
  4. import org.springframework.stereotype.Component;
  5. import java.util.List;
  6. import java.util.function.Function;
  7. @Component
  8. public class AssociationService implements Function<String, List<User>> {
  9. private final UserRepository userRepository;
  10. public AssociationService(UserRepository userRepository) {
  11. this.userRepository = userRepository;
  12. }
  13. @Override
  14. public List<User> apply(String uppercaseRequest) {
  15. List<User> users = userRepository.findAll();
  16. return users;
  17. }
  18. }

完整的代码可以在此处找到 - 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:-

  1. {
  2. &quot;errorMessage&quot;: &quot;Error creating bean with name &#39;associationService&#39; 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 &#39;userRepository&#39;: Cannot resolve reference to bean &#39;mongoTemplate&#39; while setting bean property &#39;mongoOperations&#39;; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;mongoTemplate&#39; available&quot;,
  3. &quot;errorType&quot;: &quot;org.springframework.beans.factory.UnsatisfiedDependencyException&quot;,
  4. &quot;stackTrace&quot;: [

The project has a root project which has a subproject 'GetAssociationService'

rootproject - build.gradle

  1. plugins {
  2. id &#39;org.springframework.boot&#39; version &#39;2.2.5.RELEASE&#39;
  3. id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
  4. id &#39;java&#39;
  5. id &#39;com.github.johnrengelman.shadow&#39; version &#39;5.2.0&#39;
  6. }
  7. bootJar { enabled = false }
  8. jar { enabled = true }
  9. subprojects {
  10. group = &#39;org.qmetech&#39;
  11. version = &#39;1.0.0&#39;
  12. sourceCompatibility = &#39;1.8&#39;
  13. targetCompatibility = &#39;1.8&#39;
  14. apply plugin: &#39;org.springframework.boot&#39;
  15. apply plugin: &#39;io.spring.dependency-management&#39;
  16. apply plugin: &#39;com.github.johnrengelman.shadow&#39;
  17. apply plugin: &#39;java&#39;
  18. }
  19. repositories { mavenCentral ( ) }
  20. ext { set ( &#39;springCloudVersion&#39; , &quot;Hoxton.SR3&quot; ) }
  21. dependencies {
  22. testImplementation ( &#39;org.springframework.boot:spring-boot-starter-test&#39; ) {
  23. exclude group: &#39;org.junit.vintage&#39; , module: &#39;junit-vintage-engine&#39;
  24. }
  25. }
  26. ext.libraries = [
  27. commonlibraries: [&#39;org.springframework.boot:spring-boot-starter:2.2.5.RELEASE&#39; ,
  28. &#39;org.springframework.cloud:spring-cloud-function-context&#39; ,
  29. &#39;org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE&#39; ,
  30. &#39;com.amazonaws:aws-lambda-java-log4j:1.0.0&#39; ,
  31. &#39;org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE&#39; ,
  32. &#39;org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE&#39; ,
  33. &#39;org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE&#39; ,
  34. &#39;org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE&#39; ,
  35. &#39;org.springframework.data:spring-data-mongodb:2.2.5.RELEASE&#39;
  36. ] ,
  37. ]
  38. dependencyManagement {
  39. imports {
  40. mavenBom &quot;org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}&quot;
  41. }
  42. }
  43. test { useJUnitPlatform ( ) }

ChildProject - build.gradle

  1. sourceCompatibility = 1.8
  2. targetCompatibility = 1.8
  3. repositories {
  4. mavenCentral()
  5. }
  6. springBoot {
  7. mainClassName = &#39;org.qmetech.GetAssociationService&#39;
  8. }
  9. dependencies {
  10. dependencies {
  11. compile libraries.commonlibraries
  12. implementation &#39;org.springframework.boot:spring-boot-starter-data-mongodb&#39;
  13. }
  14. }

UserRepository.java

  1. package org.qmetech.repository;
  2. import org.qmetech.domain.User;
  3. import org.springframework.data.mongodb.repository.MongoRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface UserRepository extends MongoRepository&lt;User, Integer&gt; { }

AssociationService.java

  1. package org.qmetech.service;
  2. import org.qmetech.domain.User;
  3. import org.qmetech.repository.UserRepository;
  4. import org.springframework.stereotype.Component;
  5. import java.util.List;
  6. import java.util.function.Function;
  7. @Component
  8. public class AssociationService implements Function&lt;String, List&lt;User&gt;&gt; {
  9. private final UserRepository userRepository;
  10. public AssociationService(UserRepository userRepository) {
  11. this.userRepository = userRepository;
  12. }
  13. @Override
  14. public List&lt;User&gt; apply(String uppercaseRequest) {
  15. List&lt;User&gt; users = userRepository.findAll();
  16. return users;
  17. }
  18. }

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

  1. 你需要创建 [`MongoTemplate`][1] bean因为默认配置中没有要注入的实例
  2. @Bean
  3. public MongoClient mongoClient() {
  4. return new MongoClient("localhost", 27017);
  5. }
  6. @Bean
  7. public MongoTemplate mongoTemplate(MongoClient mongoClient) throws Exception {
  8. return new MongoTemplate(mongoClient, "databaseName");
  9. }
  10. 同时不要忘记使用 `com.mongodb.client.MongoClient`它在 Spring Boot 2.2 中替代了已弃用的 `com.mongodb.MongoClient`
  11. [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.

  1. @Bean
  2. public MongoClient mongoClient() {
  3. return new MongoClient(&quot;localhost&quot;, 27017);
  4. }
  5. @Bean
  6. public MongoTemplate mongoTemplate(MongoClient mongoClient) throws Exception {
  7. return new MongoTemplate(mongoClient, &quot;databaseName&quot;);
  8. }

Also, don't forget to use com.mongodb.client.MongoClient which replaces reprecated com.mongodb.MongoClient as of Spring Boot 2.2.

huangapple
  • 本文由 发表于 2020年3月15日 20:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/60692848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定