Class CustomMongoConfig
This configuration class extends Spring Data's AbstractMongoClientConfiguration to provide custom settings for connecting to MongoDB. It is responsible for setting up the MongoDB client with the appropriate connection string from application properties and configuring the database name.
The class is annotated with @Primary to ensure it takes precedence over any auto-configured MongoDB connections. It logs the connection details during startup to aid in troubleshooting database connectivity issues.
- Since:
- 2025-05-15
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringSpecifies the name of the MongoDB database to use.com.mongodb.client.MongoClientCreates and configures the MongoDB client instance.Methods inherited from class org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
createMongoClient, mappingMongoConverter, mongoDbFactory, mongoTemplateMethods inherited from class org.springframework.data.mongodb.config.MongoConfigurationSupport
abbreviateFieldNames, autoIndexCreation, configureClientSettings, configureConverters, customConversions, fieldNamingStrategy, getInitialEntitySet, getMappingBasePackages, mongoClientSettings, mongoManagedTypes, mongoMappingContext, scanForEntities
-
Constructor Details
-
CustomMongoConfig
public CustomMongoConfig()Constructs a new CustomMongoConfig instance.This default constructor initializes the configuration class. The mongoUri field will be injected by Spring after construction.
-
-
Method Details
-
getDatabaseName
Specifies the name of the MongoDB database to use.This method is required by AbstractMongoClientConfiguration and provides the name of the database that will be used by the application. It logs the database name during initialization for debugging purposes.
- Specified by:
getDatabaseNamein classorg.springframework.data.mongodb.config.MongoConfigurationSupport- Returns:
- The name of the MongoDB database ("job_portal")
-
mongoClient
Creates and configures the MongoDB client instance.This method creates a MongoClient with the connection string from application properties. It logs the URI being used to help with debugging connection issues.
- Overrides:
mongoClientin classorg.springframework.data.mongodb.config.AbstractMongoClientConfiguration- Returns:
- A configured MongoClient instance
-