Guest User

Untitled

a guest
Jan 16th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. package com.yaskawa.DB.config;
  2.  
  3. import java.util.ArrayList;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
  7. import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
  8. import com.mongodb.Mongo;
  9. import com.mongodb.MongoClient;
  10. import com.mongodb.MongoCredential;
  11. import com.mongodb.ServerAddress;
  12.  
  13. public class SpringMongoConfiguration extends AbstractMongoConfiguration {
  14.  
  15. @Value("${spring.data.mongodb.host}")
  16. private String mongoHost;
  17.  
  18. @Value("${spring.data.mongodb.port}")
  19. private int mongoPort;
  20.  
  21. @Value("${spring.data.mongodb.database}")
  22. private String mongoDB;
  23.  
  24. @Value("${spring.data.mongodb.username}")
  25. private String username;
  26. @Value("${spring.data.mongodb.password}")
  27. private String password;
  28.  
  29. @Override
  30. public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
  31. // TODO 自動生成されたメソッド・スタブ
  32. return super.mongoMappingContext();
  33. }
  34.  
  35.  
  36. @Override
  37. @Bean
  38. public Mongo mongo() throws Exception {
  39. System.out.println("mongo host: " + mongoHost);
  40. System.out.println("mongo db: " + mongoDB);
  41.  
  42. MongoCredential credential =
  43. MongoCredential.createMongoCRCredential(username, mongoDB, password.toCharArray());
  44. ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
  45.  
  46.  
  47. return new MongoClient(serverAddress, new ArrayList<MongoCredential>() {
  48. /**
  49. *
  50. */
  51. private static final long serialVersionUID = 2121507982881939791L;
  52.  
  53. {
  54. add(credential);
  55. }
  56. });
  57. }
  58.  
  59.  
  60. @Override
  61. protected String getDatabaseName() {
  62. // TODO 自動生成されたメソッド・スタブ
  63. return mongoDB;
  64. }
  65.  
  66. <parent>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-starter-parent</artifactId>
  69. <version>1.5.9.RELEASE</version>
  70. <relativePath/> <!-- lookup parent from repository -->
  71. </parent>
  72. <properties>
  73. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  74. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  75. <java.version>1.8</java.version>
  76. </properties>
  77. <dependencies>
  78. <dependency>
  79. <groupId>org.springframework.boot</groupId>
  80. <artifactId>spring-boot-starter-web</artifactId>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.springframework.boot</groupId>
  84. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.springframework.boot</groupId>
  88. <artifactId>spring-boot-starter-tomcat</artifactId>
  89. <scope>provided</scope>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.springframework.boot</groupId>
  93. <artifactId>spring-boot-starter-test</artifactId>
  94. <scope>test</scope>
  95. </dependency>
  96. </dependencies>
  97. <build>
  98. <plugins>
  99. <plugin>
  100. <groupId>org.springframework.boot</groupId>
  101. <artifactId>spring-boot-maven-plugin</artifactId>
  102. </plugin>
  103. </plugins>
  104. </build>
  105.  
  106. spring.data.mongodb.username: user
  107. spring.data.mongodb.password: password
  108. spring.data.mongodb.host: localhost
  109. spring.data.mongodb.port: 27017
  110. spring.data.mongodb.database: test
  111. spring.data.mongodb.uri=mongodb://user:password@localhost/test★
  112.  
  113. com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='ycpuser', source='YPL', password=<hidden>, mechanismProperties={}}
  114.  
  115. Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
  116. 2018-01-16 17:44:48.183 ERROR 5748 --- [ main] o.s.boot.SpringApplication : Application startup failed
  117.  
  118. 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
  119. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  120. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  121. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  122. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  123. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  124. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  125. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  126. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  127. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  128. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  129. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  130. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  131. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  132. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  133. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  134. at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  135. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  136. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  137. at com.test.TestApplication.main(YplWebApplication.java:10) [classes/:na]
  138. 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
  139. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  140. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  141. ... 18 common frames omitted
  142. Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials must be specified
  143. at org.springframework.boot.autoconfigure.mongo.MongoProperties.createNetworkMongoClient(MongoProperties.java:237) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  144. at org.springframework.boot.autoconfigure.mongo.MongoProperties.createMongoClient(MongoProperties.java:208) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  145. at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:73) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  146. 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]
  147. 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]
  148. at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  149. at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  150. at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$74f1bfef.mongo(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
  151. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
  152. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
  153. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
  154. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
  155. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  156. ... 19 common frames omitted
Add Comment
Please, Sign In to add comment