Class CustomMongoConfig

java.lang.Object
org.springframework.data.mongodb.config.MongoConfigurationSupport
org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
me.josephsf.jobportaljosephsfeir.config.CustomMongoConfig

@Configuration @Primary public class CustomMongoConfig extends org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
Custom MongoDB configuration for the Job Portal application.

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
    Constructor
    Description
    Constructs a new CustomMongoConfig instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected String
    Specifies the name of the MongoDB database to use.
    com.mongodb.client.MongoClient
    Creates and configures the MongoDB client instance.

    Methods inherited from class org.springframework.data.mongodb.config.AbstractMongoClientConfiguration

    createMongoClient, mappingMongoConverter, mongoDbFactory, mongoTemplate

    Methods inherited from class org.springframework.data.mongodb.config.MongoConfigurationSupport

    abbreviateFieldNames, autoIndexCreation, configureClientSettings, configureConverters, customConversions, fieldNamingStrategy, getInitialEntitySet, getMappingBasePackages, mongoClientSettings, mongoManagedTypes, mongoMappingContext, scanForEntities

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      @NonNull protected String 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:
      getDatabaseName in class org.springframework.data.mongodb.config.MongoConfigurationSupport
      Returns:
      The name of the MongoDB database ("job_portal")
    • mongoClient

      @NonNull public com.mongodb.client.MongoClient 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:
      mongoClient in class org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
      Returns:
      A configured MongoClient instance