4
votes
spring.data.mongodb.uri=mongodb+srv://XXXXXX:[email protected]/test?retryWrites=true&w=majority
spring.data.mongodb.database=test

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoBootApplication': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDbFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDbFactoryConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDbFactorySupport]: Factory method 'mongoDbFactory' threw exception; nested exception is 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 com.mongodb.MongoConfigurationException: Unable to look up TXT record for host Cluster0-XXXXX.mongodb.net

2

2 Answers

3
votes

I was facing the same issue and didn't find any relevant answers. I resolved it myself. This is the issue with your port 27017 blocked by your firewall. You need to open the outbound and inbound for this port in your firewall.

Steps for Windows:

  • Go to Firewall
  • Advanced Settings
  • Inbound Rules
  • Create a new rule with New Rule under Actions tab
  • Select Port
  • Select TCP and specify port no (27015,27016,27017)
  • Allow the connection
  • Click Next
  • Add name for new Rule Finish

Follow the same steps for Outbound Rule. Once done, restart your system.

0
votes

If you're still looking for an answer:

The issue is because some ISPs block connection string. The error will occur when using default DNS server with such ISPs.

A simple test to find out if ISP's the issue, try switching over to other network and see if it still persists.

If you're lucky and switched to an ISP which doesn't block connection string then you'll connected to your cluster. If not, you can try changing the DNS configuration to use public DNS server.

for reference: https://developers.google.com//speed/public-dns/docs/using