简体   繁体   English

设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用

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

I'm trying to create a spring boot CRUD App using MongoDB.我正在尝试使用 MongoDB 创建一个 spring 引导 CRUD 应用程序。 But I'm unable to connect it to the MongoDB Atlas.但我无法将它连接到 MongoDB Atlas。 When I'm running the program the below exception is shown.当我运行程序时,会显示以下异常。 I'm trying to connect my application to MongDB Atlas clusters using the connection URL generated in the Mongo Atlas我正在尝试使用 Mongo Atlas 中生成的连接 URL 将我的应用程序连接到 MongDB Atlas 集群

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo' defined in com.example.mongo.repo.EmployeeRepo defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (@) then it must be urlencoded
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1415) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:608) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923) ~[spring-context-5.3.3.jar:5.3.3]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.3.jar:5.3.3]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar:2.4.2]
    at com.example.mongo.MongoApplication.main(MongoApplication.java:11) ~[classes/:an]

My Application class:我的应用程序 class:

@SpringBootApplication
public class MongoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MongoApplication.class, args);
    }

}

Controller class: Controller class:

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeRepo employeeRepo;

    @Autowired
    private SequenceGeneratorService sequenceGeneratorService;

    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeRepo.findAll();
    }

    @GetMapping("/employees/{id}")
    public Employee getEmployee(@PathVariable long id){
        Employee employee = employeeRepo.findById(id).get();
        return employee;
    }

    @PostMapping("/employees")
    public Employee addEmployee(@RequestBody Employee employee){
        employee.setId(sequenceGeneratorService.generateSequence(Employee.SEQUENCE_NAME));
        return employeeRepo.save(employee);
    }
}

Repository:存储库:

public interface EmployeeRepo extends MongoRepository<Employee, Long> {
}

Model class: Model class:

@Document(collation = "Employee")                                                                            
public class Employee {                                                                                      
                                                                                                             
    @Transient                                                                                               
    public static final String SEQUENCE_NAME = "users_sequence";                                             
                                                                                                             
    @Id                                                                                                      
    private long id;                                                                                         
                                                                                                             
    @Indexed(unique = true)                                                                                  
    private String firstName;                                                                                
    private String lastName;                                                                                 
                                                                                                             
    @Indexed(unique = true)                                                                                  
    private String email;                                                                                    
                                                                                                             
    public Employee() {                                                                                      
    }                                                                                                        
                                                                                                             
    public Employee(long id, String firstName, String lastName, String email) {                              
        this.id = id;                                                                                        
        this.firstName = firstName;                                                                          
        this.lastName = lastName;                                                                            
        this.email = email;                                                                                  
    }                                                                                                        
                                                                                                             
    public Employee(String firstName, String lastName, String email) {                                       
        this.firstName = firstName;                                                                          
        this.lastName = lastName;                                                                            
        this.email = email;                                                                                  
    }                                                                                                        
            
    /** Getters and setters **/
    
}

Application Properties:应用属性:

spring.data.mongodb.uri=mongodb+srv://root:Luxan@22@testcluster.4ebwy.mongodb.net/EmployeeDB?retryWrites=true&w=majority
                                                                                             

The connection string contains invalid user information.连接字符串包含无效的用户信息。 If the username or password contains a colon (:) or an at-sign (@) then it must be urlencoded如果用户名或密码包含冒号 (:) 或符号 (@),则必须进行 urlencoded

In your mongo connect uri, the password 'Luxan@22' contains a '@' character should be urlencoded.在您的 mongo 连接 uri 中,密码 'Luxan@22' 包含一个 '@' 字符应该是 urlencoded。 So you can write just like 'Luxan%4022'所以你可以写成 'Luxan%4022'

In EmployeeRepo interface add the @Repository annotation在 EmployeeRepo 接口中添加@Repository注解

暂无
暂无

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

相关问题 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' 在部署 AWS Lambda 时设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda 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' 无法自动装配 MongoOperations bean - Cannot autowire MongoOperations bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM