io.github.autsia.crowly.config.PersistenceConfig.java Source code

Java tutorial

Introduction

Here is the source code for io.github.autsia.crowly.config.PersistenceConfig.java

Source

/*
 * Copyright 2014 Dmytro Titov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.autsia.crowly.config;

import com.mongodb.Mongo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * Created by Dmytro on 3/23/2014 at 14:43
 * Project: crowly
 */
@Configuration
@EnableMongoRepositories(basePackages = "io.github.autsia.crowly.repositories")
public class PersistenceConfig extends AbstractMongoConfiguration {

    @Value("${host}")
    private String host;

    @Value("${port}")
    private int port;

    @Value("${database}")
    private String database;

    @Value("${userlogin}")
    private String user;

    @Value("${password}")
    private String password;

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

    @Override
    public SimpleMongoDbFactory mongoDbFactory() throws Exception {
        UserCredentials userCredentials = new UserCredentials(user, password);
        return new SimpleMongoDbFactory(mongo(), getDatabaseName(), userCredentials);
    }

    @Override
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }

    @Override
    public Mongo mongo() throws Exception {
        return new Mongo(host, port);
    }

}