41025
こちらを参考にしています。  
<https://qiita.com/KoheiKanagu/items/2ba5f49632d9868159fc#group>

◆SpringMongoConfiguration.java

```

    package com.yaskawa.DB.config;
    
    import java.util.ArrayList;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
    import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
    import com.mongodb.Mongo;
    import com.mongodb.MongoClient;
    import com.mongodb.MongoCredential;
    import com.mongodb.ServerAddress;
    
    public class SpringMongoConfiguration extends AbstractMongoConfiguration {
    
        @Value("${spring.data.mongodb.host}")
        private String mongoHost;
    
        @Value("${spring.data.mongodb.port}")
        private int mongoPort;
    
        @Value("${spring.data.mongodb.database}")
        private String mongoDB;
    
        @Value("${spring.data.mongodb.username}")
        private String username;
        @Value("${spring.data.mongodb.password}")
        private String password;
    
        @Override
        public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
            // TODO 自動生成されたメソッド・スタブ
            return super.mongoMappingContext();
        }
    
    
        @Override
        @Bean
        public Mongo mongo() throws Exception {
            System.out.println("mongo host: " + mongoHost);
            System.out.println("mongo db: " + mongoDB);
    
            MongoCredential credential =
                    MongoCredential.createMongoCRCredential(username, mongoDB, password.toCharArray());
            ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
    
    
            return new MongoClient(serverAddress, new ArrayList<MongoCredential>() {
                /**
                 * 
                 */
                private static final long serialVersionUID = 2121507982881939791L;
    
                {
                    add(credential);
                }
            });
        }
    
    
        @Override
        protected String getDatabaseName() {
            // TODO 自動生成されたメソッド・スタブ
            return mongoDB;
        }
    
    }
    
```

◆pom.xml

```

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
```

◆application.properties

```

    spring.data.mongodb.username: user
    spring.data.mongodb.password: password
    spring.data.mongodb.host: localhost
    spring.data.mongodb.port: 27017
    spring.data.mongodb.database: test
    spring.data.mongodb.uri=mongodb://user:password@localhost/test★
    
```

★がないと以下のエラーが発生していました

```

    com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='ycpuser', source='YPL', password=<hidden>, mechanismProperties={}}
    
```

上記の状態でSpringbootアプリケーションの実行を行うと以下のようなエラーが出てしまいます。  
何か設定で足りない部分などありますでしょうか？

```

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    2018-01-16 17:44:48.183 ERROR 5748 --- [           main] o.s.boot.SpringApplication               : Application startup failed
    
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials must be specified
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at com.test.TestApplication.main(YplWebApplication.java:10) [classes/:na]
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials must be specified
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        ... 18 common frames omitted
    Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials must be specified
        at org.springframework.boot.autoconfigure.mongo.MongoProperties.createNetworkMongoClient(MongoProperties.java:237) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.autoconfigure.mongo.MongoProperties.createMongoClient(MongoProperties.java:208) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:73) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$74f1bfef.CGLIB$mongo$1(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$74f1bfef$$FastClassBySpringCGLIB$$fa24578d.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$74f1bfef.mongo(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
        ... 19 common frames omitted
    
```
CC BY-SA 3.0
2018-01-16T08:53:27.123
41010
2018-01-17T04:43:23.293
2018-01-17T04:43:23.293
21092
12842
question
spring-boot mongodb
SpringbootでMongoDB接続したい
`application.properties` はプロパティファイルなので、 `=`
区切りで書かないと認識されないですね。★がないとエラーが発生するのは、その行だけが正しく `=` 区切りで書かれていたためです。
CC BY-SA 3.0
2018-01-17T04:36:24.130
41025
2018-01-17T04:36:24.130
21092
41010
answer