반응형
728x90
반응형
MongoDB Atlas 설정
https://devfunny.tistory.com/850
MongoDB Atlas 연동
1) [Connect] 클릭
2) [Drivers] 선택
3) [Driver], [Version] 선택
주의할점
제공받은 code
mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/?retryWrites=true&w=majority
database명을 반드시 추가하여 아래 application.yml에 작성해야한다.
mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/<database>?retryWrites=true&w=majority
database명 작성하지 않았을 경우 오류 발생
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:659) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:647) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.2.jar:3.1.2]
at com.studyolle.StudyolleServerApplicationKt.main(StudyolleServerApplication.kt:17) ~[main/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.2.jar:3.1.2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-6.0.11.jar:6.0.11]
... 22 common frames omitted
Caused by: java.lang.IllegalArgumentException: Database name must not be empty
at org.springframework.util.Assert.hasText(Assert.java:294) ~[spring-core-6.0.11.jar:6.0.11]
at org.springframework.data.mongodb.core.MongoDatabaseFactorySupport.<init>(MongoDatabaseFactorySupport.java:68) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:75) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:64) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
at org.springframework.boot.autoconfigure.data.mongo.MongoDatabaseFactoryConfiguration.mongoDatabaseFactory(MongoDatabaseFactoryConfiguration.java:52) ~[spring-boot-autoconfigure-3.1.2.jar:3.1.2]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.11.jar:6.0.11]
... 23 common frames omitted
Project 설정
build.gradle.kts
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
application.yml
spring:
...
data:
mongodb:
uri: mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/studyolle?retryWrites=true&w=majority
StudyolleServerApplication.kt
@SpringBootApplication
@EnableJpaRepositories
@EnableMongoRepositories
class StudyolleServerApplication
fun main(args: Array<String>) {
runApplication<StudyolleServerApplication>(*args)
}
Sample 데이터 넣어보기
▶ 이전 포스팅 참고
https://devfunny.tistory.com/851
1) studyolledb 데이터베이스의 account_login 컬렉션에 데이터 등록
SpringBoot 간단한 코드 생성
AccountLogin.kt
@Getter
@Setter
@Document(collection = "account_login")
class AccountLogin(
@Id
val _id: String,
val nickname: String? = null
) {
}
AccountLoginRepository.kt
interface AccountLoginRepository : MongoRepository<AccountLogin, String> {
}
AccountLoginReaderImpl.kt
@Component
class AccountLoginReaderImpl(
private val accountLoginRepository: AccountLoginRepository
): AccountLoginReader {
override fun findAll(): Any {
return accountLoginRepository.findAll()
}
}
조회 결과 확인
반응형
'Coding > Spring' 카테고리의 다른 글
[Spring] 트랜잭션 관리 (@Transactional, 동적 프록시, JDK Dynamic Proxy vs GCLIB) (1) | 2024.01.01 |
---|---|
SpringBoot 3.x 버전 이상 프로젝트에 Swagger 적용 오류 해결 및 springdoc-openapi 적용 (0) | 2023.08.20 |
DispatcherServlet 요청 흐름 (0) | 2022.12.13 |
Spring + Mybatis 활용 원리 (1) | 2022.12.01 |
Maven + MockMvc 환경에서 Spring Rest Docs 설정하기 (2) | 2022.09.14 |