cannot resolve reference to bean 'mongotemplate' while setting bean property 'mongooperations 

In this post, we are going to provide possible solutions for the error "Cannot resolve reference to bean 'mongotemplate' while setting bean property 'mongooperations" you can try below approach

The error message indicates that there is an issue resolving the reference to the bean named 'mongotemplate' while setting the bean property 'mongooperations'. Here are some steps you can take to resolve this issue:

  1. Check Bean Configuration: Ensure that you have defined a bean named 'mongotemplate' in your Spring configuration. The 'mongotemplate' bean is typically used for MongoDB operations, and it seems that another bean is trying to reference it. Here's an example of how you might define a MongoDB template bean:

    @Bean public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory, MappingMongoConverter mappingMongoConverter) { return new MongoTemplate(mongoDbFactory, mappingMongoConverter); }
  2. Correct Property Name: Double-check that the property name being set is indeed 'mongooperations' and not a typo. Make sure that the property name matches the actual property in the class or configuration you are trying to set.

  3. Ensure Component Scanning: If you are relying on component scanning to pick up your MongoDB configuration class, make sure that the package containing the configuration is being scanned. You can use the @ComponentScan annotation in your main application class:

    @SpringBootApplication @ComponentScan(basePackages = "com.example") // Adjust package name accordingly public class YourApplication { public static void main(String[] args) {, args); } }
  4. Check Import Statements: If you are using a separate configuration class, ensure that you have imported the necessary classes for MongoDB configuration, such as MongoTemplate, MongoDatabaseFactory, and MappingMongoConverter.

  5. Verify Dependencies: Make sure that you have the necessary dependencies for MongoDB in your project. If you're using Maven or Gradle, ensure that you have the correct versions of the MongoDB dependencies.

Solution 2: You need to create beans of MongoTemplate since there are none to be injected by default configuration.

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

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


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

Solution 3:The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (@), then it must be URL encoded.

In your MongoDB connection URI, the password 'ashok@22' contains an '@' character, which should be URL encoded. So, you can write it as 'lashok%4022'.

This issue is caused by referencing a bean using the wrong name.

To fix it, override the name of the bean created by the @Service annotation like this:

public class CustomUserDetails implements UserDetailsService

By default, Spring will lowercase the first character of the component's name – from 'CustomUserDetails' to 'customUserDetails'. You can retrieve this component with the name 'customUserDetails'.

You can also modify the following code if you don't want to override the default name:

    <security:authentication-manager alias="authenticationManager">
    <security:authentication-provider user-service-ref="customUserDetails" />

Another possible cause of the issue is that you have defined <annotation-driven> in the servlet XML, which is in the web application context where your service bean is initialized. However, you have defined security-context in the main application context. To resolve this, create a new file, give it any name, and move the relevant configuration to that file.

Refer to this link for a better understanding of this concept.

You only need the following dependency, and it will include all the necessary JARs:


The error you are encountering, java.lang.NoSuchMethodError, is related to the ClassTypeInformation class. Please verify whether spring-data-commons-1.12.3.RELEASE.jar is present after you build your project. If it's not, try cleaning up your build environment and updating the Maven project.

Remove the @Profile("container") annotation in MongoDBConfiguration.

Explanation: The presence of @Profile indicates that the class will not be instantiated by Spring unless you are running Spring with that specific profile. It seems likely that you are not setting the property to "container" when you run your application via Tomcat or during integration testing.

If you prefer to keep the @Profile("container") annotation, ensure that you set the profile to "container". There are multiple ways to do this. One quick and easy method is to use Java system properties, for example,, when executing your integration tests or running your application in Tomcat.