Error creating bean with name 'mongoTemplate' while connecting mongodb through Spring
You only need below dependency and it will bring you all needed jars.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
The error java.lang.NoSuchMethodError
you are getting is for ClassTypeInformation
class. Please check whether spring-data-commons-1.12.3.RELEASE.jar is present after you build your project. If not, then try cleaning up your build environment and update maven project.
A little late to the party, but here is what you need.
If you are trying to use a custom data manipulation rather than using the default inbuilt mongo repositories
, then you need a mongoTemplate
(kind of jdbc template but lets you define your own implementation of the client, i.e, the mongo client
, in this case) and optionally mongoOperations
on top of it(Mongo Operations
is kind of a wrapper on top of the mongoTemplate
)
You need the following dependencies - pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> </dependency>
MongoConfig.java
@PropertySource("classpath:application.properties")public class MongoConfig{ @Value("${spring.data.mongodb.host}") private String mongoHost; @Value("${spring.data.mongodb.port}") private String mongopPort; @Value("${spring.data.mongodb.database}") private String mongoDB; /*Client vs FactoryClient * * Factory bean that creates the com.mongodb.MongoClient instance * * Classes attributed with @Repostiory may throw mongo related exceptions. Declaring an instance of MonogClientFactoryBean * helps in translating them to spring data exceptions which can then be caught using @ExceptionHandling * */ public @Bean MongoClientFactoryBean mongo() throws Exception { MongoClientFactoryBean mongo = new MongoClientFactoryBean(); mongo.setHost("localhost"); MongoClientOptions clientOptions = MongoClientOptions.builder().applicationName("FeddBackAPI_DB") .connectionsPerHost(2000) .connectTimeout(4000) //.maxConnectionIdleTime(1000000000) .maxWaitTime(3000) .retryWrites(true) .socketTimeout(4000) .sslInvalidHostNameAllowed(true)//this is very risky .build(); mongo.setMongoClientOptions(clientOptions); return mongo; }}
DataSourceConfig.java
@Configuration@Import(value=MongoClientFactory.class)public class DataSourceConfig { @Autowired Mongo mongo; @Autowired Environment env; @Bean public String test() { System.out.println("mongo"+mongo); return "rer"; } @Bean @Qualifier("customMongoTemplate") public MongoTemplate mongoTemplate() { //MongoClient is the actual pool used by mongo. Create it using client factory then, autoclosing of threads are handled on its own MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test"); MongoTemplate template = new MongoTemplate(factory); return template; } @Bean @Qualifier(value="customMongoOps") public MongoOperations mongoOps() { MongoOperations ops = mongoTemplate(); return ops; } @Bean public MongoDbFactory factory() { MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test"); return factory; } // @Bean// public GridFsTemplate gridFsTemplate() {// return new GridFsTemplate(mongo, converter)// } }
This should successfully create the mongoTemplate
and mongoOperations
and you should be able to make use of them in your DAO or service and access them.
PersonService.java
@Service public class PersonService { @Autowired private PersonRepository personRepo; @Autowired PersonSequenceServiceImpl seqService; @Autowired @Qualifier(value="customMongoOps") MongoOperations mongoOps; public List<Person> findAllPersons() { return personRepo.findAll(); } public List<Person> createAndFindAllPersons() { Person p1 = new Person( "another1", "ll1", 30); Person p2 = new Person( "another2", "ll2", 30); if(!mongoOps.collectionExists(Person.class)) { mongoOps.dropCollection("Person_table"); } //return personRepo.save(person); System.out.println("P1 data before inserting:"+p1); mongoOps.insert(Arrays.asList(p1,p2), Person.class); //mongoOps.dropCollection(Person.class); return mongoOps.findAll(Person.class); } }
I know it's not a technically justified solution. But after trying several alternatives, I just closed Eclipse and deleted all the .m2 folder content. Then, I retried to import the Project ina new Workspace and compiled. Surprise! This time it worked :)Sometimes rebooting works ;)