cloud.simple.RuleEngineApplication.java Source code

Java tutorial

Introduction

Here is the source code for cloud.simple.RuleEngineApplication.java

Source

package cloud.simple;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.async.DeferredResult;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import cloud.simple.db.MongoDB;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* @brief 
* @details 
* @see 
* @author chunzhao.li
* @version v1.0.0.0
* @date 2016928?1:54:46
* @note spring boot ??mongo3.Xdemo?mongo?javabean??mongodatabase,?mongo?
*        springboot?mongo???????3.X?
* 
*/
@SpringBootApplication
@EnableSwagger2
//@EnableDiscoveryClient
public class RuleEngineApplication {

    @Autowired
    private Environment env;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(RuleEngineApplication.class, args);
    }

    @Bean
    public MongoDatabase dataSource() {
        String servers = env.getProperty("spring.data.mongodb.custom.service");
        String databaseName = env.getProperty("spring.data.mongodb.database");

        List<ServerAddress> seeds = new ArrayList<ServerAddress>();
        String[] servers1 = servers.split(",");
        for (String server : servers1) {
            String[] server1 = server.split(":");
            seeds.add(new ServerAddress(server1[0], Integer.parseInt(server1[1])));
        }

        Builder builder = MongoClientOptions.builder();
        builder.socketKeepAlive(true);
        builder.readPreference(ReadPreference.secondaryPreferred());
        MongoClientOptions options = builder.build();

        @SuppressWarnings("resource")
        MongoClient mongoClient = new MongoClient(seeds, options);
        return mongoClient.getDatabase(databaseName);
    }

    @Bean
    public MongoDB mongoDB() {
        return new MongoDB();
    }

    /**?*/
    @Bean
    public Docket defaultApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("default")
                .genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false)
                .forCodeGeneration(true).pathMapping("/")// base??paths
                .select().paths(PathSelectors.any())//?
                .build().apiInfo(defaultApiInfo());
    }

    private ApiInfo defaultApiInfo() {
        ApiInfo apiInfo = new ApiInfo("LCMAPI", //
                "?", //?
                "1.0", //
                "NO terms of service", "chunzhao.li", //
                "The Accenture License, Version 1.0", //
                "http://www.accenture.cn"//
        );
        return apiInfo;
    }
}