【问题标题】:flapdoodle.embed.mongo gets always started with Spring Boot Main application in Eclipse, how to removefladdoodle.embed.mongo 总是从 Eclipse 中的 Spring Boot Main 应用程序开始,如何删除
【发布时间】:2021-03-09 13:54:34
【问题描述】:

我有一个问题。一个简单的 Spring Boot 应用程序适用于现有的 MongoDB 配置。 对于集成测试,我使用 flapdoodle 配置添加了嵌入式 mongodb 所需的配置。所有单元测试都得到正确执行。当我运行主 Spring Boot 应用程序时,默认情况下它会考虑 fladdoodle 嵌入的 mongodb 配置。结果,嵌入的 mongodb 永远不会退出,并且在运行 junit 测试用例时,它仍然运行。我在下面提供代码sn-p。

每当我启动 Spring Boot 主应用程序时,它仍然运行嵌入的 mongodb。我总是在控制台中看到以下几行。

Download PRODUCTION:Windows:B64 START
Download PRODUCTION:Windows:B64 DownloadSize: 231162327
Download PRODUCTION:Windows:B64 0% 1% 2% 3% 4% 5% 6% 7% 8% 

我提供了运行主 Spring Boot 应用程序时应该获取的 Mongodb 配置代码。

@Slf4j
@Configuration
public class NoSQLAutoConfiguration {

    @Autowired
    private NoSQLEnvConfigProperties configProperties;

    /**
     * Morphia.
     *
     * @return the morphia
     */
    private Morphia morphia() {
        final Morphia morphia = new Morphia();
        morphia.mapPackage(DS_ENTITY_PKG_NAME);
        return morphia;
    }


    @Bean
    public Datastore datastore(@Autowired @Qualifier("dev") MongoClient mongoClient) {
        String dbName = configProperties.getDatabase();
        final Datastore datastore = morphia().createDatastore(mongoClient, dbName);
        datastore.ensureIndexes();

        return datastore;
    }

    /**
     * Mongo client.
     *
     * @return the mongo client
     */
    @Primary
    @Bean(name = "dev")
    public MongoClient mongoClient() {
        MongoClient mongoClient = null;
        String dbHost = configProperties.getHost();
        int dbPort = configProperties.getPort();
        String database = configProperties.getDatabase();
        log.debug("MongDB Host: {} - MongoDB Port: {}", dbHost, dbPort);
        List<ServerAddress> serverAddresses = new ArrayList<>();
        serverAddresses.add(new ServerAddress(dbHost, dbPort));
        MongoClientOptions options = getMongoOptions();
        String dbUserName = configProperties.getMongodbUsername();
        String encRawPwd = configProperties.getMongodbPassword();
        char[] dbPwd = null;
        try {
            dbPwd = Util.decode(encRawPwd).toCharArray();
        } catch (Exception ex) {
            // Ignore exception
            dbPwd = null;
        }
        Optional<String> userName = Optional.ofNullable(dbUserName);
        Optional<char[]> password = Optional.ofNullable(dbPwd);

        if (userName.isPresent() && password.isPresent()) {
            MongoCredential credential = MongoCredential.createCredential(dbUserName, database, dbPwd);
            List<MongoCredential> credentialList = new ArrayList<>();
            credentialList.add(credential);
            mongoClient = new MongoClient(serverAddresses, credentialList, options);
        } else {
            log.debug("Connecting to local Mongo DB");
            mongoClient = new MongoClient(dbHost, dbPort);
        }
        return mongoClient;
    }

    
    private MongoClientOptions getMongoOptions() {
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.maxConnectionIdleTime(configProperties.getMongodbIdleConnection());
        builder.minConnectionsPerHost(configProperties.getMongodbMinConnection());
        builder.connectTimeout(configProperties.getMongodbConnectionTimeout());
        return builder.build();
    }

}

对于集成测试,我有嵌入式 mongodb 的配置,它是 src/test 的一部分。

@TestConfiguration
public class MongoConfiguration implements InitializingBean, DisposableBean {

    MongodExecutable executable;
    private static final String DBNAME = "embeded";
    private static final String DBHOST = "localhost";
    private static final int DBPORT = 27019;

    @Override
    public void afterPropertiesSet() throws Exception {
        IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                .net(new Net(DBHOST, DBPORT, Network.localhostIsIPv6())).build();

        MongodStarter starter = MongodStarter.getDefaultInstance();
        executable = starter.prepare(mongodConfig);
        executable.start();
    }

    private Morphia morphia() {
        final Morphia morphia = new Morphia();
        morphia.mapPackage(DS_ENTITY_PKG_NAME);
        return morphia;
    }

    @Bean
    public Datastore datastore(@Autowired @Qualifier("test") MongoClient mongoClient) {
        
        final Datastore datastore = morphia().createDatastore(mongoClient, DBNAME);
        datastore.ensureIndexes();

        return datastore;
    }

    
    @Bean(name = "test")
    public MongoClient mongoClient() {
        return new MongoClient(DBHOST, DBPORT);
    }

    @Override
    public void destroy() throws Exception {
        executable.stop();
    }
}

请帮助我在eclipse中运行Spring Boot主应用程序时如何删除这个嵌入的mongo配置。

我也在下面提供我的主要应用程序。

@EnableAspectJAutoProxy
@EnableSwagger2
@SpringBootApplication(scanBasePackages = { "com.blr.app" })
public class ValidationApplication {

    /**
     * The main method. f
     * 
     * @param args the arguments
     */
    public static void main(String[] args) {
        SpringApplication.run(ValidationApplication.class, args);
    }

}

【问题讨论】:

    标签: java spring mongodb spring-boot


    【解决方案1】:

    我看到您没有向MongoConfiguration 类添加任何配置文件的代码。在 Eclipse 构建期间,这个类也被 Spring 框架拾取。将以下行添加到此类中,以便在运行 Spring Boot 测试时选择该类,并在运行主 Spring Boot 应用程序时选择实际的 Mongo 配置文件。这就是 Spring 提出概念 Profile 的原因。为不同的环境适当添加配置文件。

    @Profile("test")
    @ActiveProfiles("test")
    

    所以最终的代码将如下所示。

    @Profile("test")
    @ActiveProfiles("test")
    @TestConfiguration
    public class MongoConfiguration implements InitializingBean, DisposableBean {
       ...
       ...
    }
    

    【讨论】:

    • 让我检查一下两个都运行。
    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 2017-03-28
    • 2019-07-17
    • 1970-01-01
    • 2022-01-10
    • 2019-08-19
    • 2016-07-17
    • 2016-04-27
    相关资源
    最近更新 更多