1

Spring boot application properties needs to follow convention from https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html when we use any DB like cassandra/mongo. In case if we want to declare our own properties for DB setup instead of spring-boot convention, what are all the steps we need to do for setting up DB?

Ayan
  • 515
  • 4
  • 9
  • 20

2 Answers2

1

You can do this: Spring boot - custom variables in Application.properties

or you can just create your own property in your application.properties file like:

my.property.someDb.hostname=http://wherever.comand then reference to it in your code like:

@Value("${my.property.someDb.hostname}")
private String someDbHostname;

Update 1:

If you want to create the MongoDb with your own properties you have to define the right Java Beans in an @Configuration file. For MongoDB it could look like the following:

@Configuration
public class MyMongoConfig extends AbstractMongoConfiguration{ 

  @Value("${my.property.someDb.hostname}")
  private String someDbHostname;

  @Value("${my.property.someDb.myOwnPortDefinition}")
  private int myOwnPortDefinition;

  @Value("${my.property.someDb.myDatabasename}")
  private String myDatabasename;

  @Override
  protected String getDatabaseName() {
    return myDatabasename;
  }

  @Override
  @Bean
  public Mongo mongo() throws Exception{
    return new MongoClient(someDbHostname, myOwnPortDefinition );
  }

  @Bean
  public MongoTemplate mongoTemplate() throws Exception{
    return new MongoTemplate(mongo(), getDatabaseName());
  }    
}
rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • I am not asking about how to use property in spring-boot app, I want to know what steps we need to do for DB setup like entity manager, transaction manager etc when I use own properties instead of properties from that list, and how should we deal with them. – Ayan Dec 16 '17 at 05:34
  • then please specify the database and the used driver for it. Are you accessing e.g. MongoDb through Morphia/Spring Data/Plain mongo driver? – rieckpil Dec 16 '17 at 05:38
  • lets say it is mongo db with spring data. – Ayan Dec 16 '17 at 05:40
1

These are the essential steps you need in order to get a data source like Jdbc, mongodb set up in Spring Boot

  • Need a @Configuration class that has transaction management enabled on it
  • Read the environment properties for the datasource i.e. dataSource url, username, password etc.
  • Create beans for datasource, session factory, transaction manager etc.
  • Once all of the above setup, use this @Configuration in your consumer to initialize the spring application context

Here are some snippets of wiring mongodb datasource in spring boot

DataSourceConfiguration.java

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.example.xyz"})
public class DatabaseEntityConfiguration {

    public static final String DATABASE_ENTITY_DATA_SOURCE = "databaseDataSource";
    public static final String DATABASE_HIBERNATE_PROPERTIES = "databaseHibernateProperties";
    public static final String DATABASE_ENTITY_SESSION_FACTORY = "databaseSessionFactory";
    public static final String DATABASE_ENTITY_TRANSACTION_MANAGER = "databaseTransactionManager";
    public static final String DATABASE_ENTITY_DB_CONFIG_DAO = "dmdatabaseDbConfigDao";
    public static final String DATABASE_ENTITY_DB_CONFIG_SERVICE = "dmdatabaseDbConfigService";

    private static final String ENTITY_PACKAGE = "com.example.xyz.database.entity";

    @Autowired
    private org.springframework.core.env.Environment environment;

    @Bean(name = DATABASE_ENTITY_DATA_SOURCE)
    public DataSource databaseEntitydataSource() throws PropertyVetoException {

        // mongodb properties
        String driverClass = environment.getProperty("databaseEntity.mongodb.driverClassName");
        String mongodbUrl = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.url");
        String user = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.username");
        String password = environment.getProperty("databaseEntity.mongodb.dmdatabaseDataSource.password");

        Preconditions.checkArgument(StringUtils.isNotBlank(driverClass), "The property mongodb driverClass must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(mongodbUrl), "The property mongodb mongodbUrl must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(user), "The property mongodb user must not be null or blank");
        Preconditions.checkArgument(StringUtils.isNotBlank(password), "The property mongodb password must not be null or blank");

        dataSource.setDriverClass(driverClass);
        dataSource.setmongodbUrl(mongodbUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        return dataSource;
    }

    @Bean(name = DATABASE_ENTITY_SESSION_FACTORY)
    public AnnotationSessionFactoryBean databaseEntitySessionFactory() throws PropertyVetoException {
        AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
        annotationSessionFactoryBean.setDataSource(databaseEntitydataSource());
        annotationSessionFactoryBean.setPackagesToScan(ENTITY_PACKAGE);
        annotationSessionFactoryBean.setAnnotatedClasses(DBConfig.class);

        annotationSessionFactoryBean.setHibernateProperties(databaseEntityHibernateProperties());
        return annotationSessionFactoryBean;
    }

    @Bean(name = DATABASE_ENTITY_TRANSACTION_MANAGER)
    public HibernateTransactionManager databaseEntityTransactionManager() throws PropertyVetoException {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(databaseEntitySessionFactory().getObject());
        return transactionManager;
    }
}
Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25