본문 바로가기
공부/Trouble Shooting

(Kotlin)Multi-Module-Gradle 프로젝트 첫 실행 시 Parameter 0 of constructor in com.zerobase.api.test.TestService required a bean of type 오류

by son_i 2023. 10. 13.
728x90

현재까지 진행한 것

최상위 폴더에서 build.gradle.kts 세팅

domain 모듈에서 build.gradle.kts(:domain), appllication-domain.yml에 DB접속정보 설정, Entity, Repository 생성

api 모듈에서 build.gradle.kts(:api), application.yml에 domain과 같은 설정 넣어주는 코드 작성. 

  그리고 db접근이 잘 되나 보려고test 패키지에 TestController, TesetDto, TesetService 생성.

 

ApiApplication.kt 생성 후 실행하니 다음과 같은 오류 발생

Description: Parameter 0 of constructor in com.zerobase.api.test.TestService required a bean of type 'com.zerobase.domain.repository.UserInfoRepository' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.zerobase.domain.repository.UserInfoRepository' in your configuration. Process finished with exit code 1

딱 봐도 빈 등록이 안 된다는 오류 같아서 원래 강사님 설명에는 없었던

두 개의 Repository에 @Repository 어노테이션 붙여주고 메인 클래스있는 곳에서

@SpringBootApplication
@EntityScan(basePackages = ["com.zerobase.domain"])
@ComponentScan(basePackages = ["com.zerobase"])
@EnableJpaRepositories(basePackages = ["com.zerobase.repository"])
@EnableCaching
class ApiApplication

fun main(args: Array<String>) {
    runApplication<ApiApplication>(*args)
}

이렇게까지 다 해주었는데 자꾸 같은 오류가 발생했다...

언어도 낯설고 어려운데 하나 할 때마다 자꾸 오류가 발생하니...

 

ㅋ ㅋ 근데 방금 포스팅 하느라 코드 가져오면서 발견한 건데

@EnableJpaRepositories에 basePackages 주소로 "com.zerobase.domain.domain.repository"를 해줘야 한다 !

내가 패키지를 그렇게 만들었기 때문 .. ㅋ ㅋㅋ

 

이렇게 바꾸니까 정상 동작 !

 

------------------

여기서 하나씩 지워보면서 어떤 것 때문에 안 된 건지 체크해보려고 한다.

 

1. 일단 Repository 파일에 @Repository가 안 붙어도 된다 !

자동으로 이 패키지를 스캔하면서 빈으로 등록이 되는 듯 ???

 

근데 인제 Repository파일에 @Repository가 붙어도 현재 main이 있는 api 모듈에서는 거기를 찾을 수가 없나보다

@EnableJpaRepositories(basePackages = ["com.zerobase.domain.repository"])

이 부분은 필수라는 것을 알았고 

 

2. TestController와 TestService에서 의존성이 필요한 부분들

private val testService: TestService
private val userInfoRepository: UserInfoRepository

에 @Autowired를 넣어주었는데 이 부분을 빼도 정상동작 한다.

 

3. ComponentScan을 빼면 ? 

정상동작한다.

그치만 나중을 위해 일단 넣어놔주도록 한다.

 

4. @EntityScan을 빼면 ? 

오류가 난다.

무슨 오류인가 보면 testController Bean을 등록하는데 오류가 났다고 하고

testService도 , userInfoRepository도 bean등록에 오류가 났다고 한다. 

마지막에는 UserInfo라는 Entity를 찾지 못 한 것 같다.

 

조금만 생각해봐도 이해가 되는게 EntityScan을 빼서

UserInfo라는 Entity를 찾지 못 하므로

UserInfo라는 Entity에 의존성을 가지고 있는 userInfoRepository도 생성이 되지 못 했을 것이고

userInfoRepository를 의존하는 userService도 생성 못 하고

userService를 의존하는 controller도 빈으로 생성이 되지 못 한 것이다 !

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController' defined in file [C:\spring_prac\fintech\api\build\classes\kotlin\main\com\zerobase\api\test\TestController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testService' defined in file [C:\spring_prac\fintech\api\build\classes\kotlin\main\com\zerobase\api\test\TestService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userInfoRepository' defined in com.zerobase.domain.repository.UserInfoRepository defined in @EnableJpaRepositories declared on ApiApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.zerobase.domain.domain.UserInfo at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:801) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:224) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:921) ~[spring-context-5.3.30.jar:5.3.30] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.30.jar:5.3.30] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.16.jar:2.7.16] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.16.jar:2.7.16] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.16.jar:2.7.16] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.16.jar:2.7.16] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.16.jar:2.7.16] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.16.jar:2.7.16] at com.zerobase.api.ApiApplicationKt.main(ApiApplication.kt:20) ~[main/:na] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testService' defined in file [C:\spring_prac\fintech\api\build\classes\kotlin\main\com\zerobase\api\test\TestService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userInfoRepository' defined in com.zerobase.domain.repository.UserInfoRepository defined in @EnableJpaRepositories declared on ApiApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.zerobase.domain.domain.UserInfo at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:801) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:224) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:911) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788) ~[spring-beans-5.3.30.jar:5.3.30] ... 19 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userInfoRepository' defined in com.zerobase.domain.repository.UserInfoRepository defined in @EnableJpaRepositories declared on ApiApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.zerobase.domain.domain.UserInfo at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:911) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788) ~[spring-beans-5.3.30.jar:5.3.30] ... 33 common frames omitted Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.zerobase.domain.domain.UserInfo at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:583) ~[hibernate-core-5.6.15.Final.jar:5.6.15.Final] at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.6.15.Final.jar:5.6.15.Final] at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:233) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:182) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:165) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:76) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:325) ~[spring-data-commons-2.7.16.jar:2.7.16] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.7.16.jar:2.7.16] at org.springframework.data.util.Lazy.getNullable(Lazy.java:231) ~[spring-data-commons-2.7.16.jar:2.7.16] at org.springframework.data.util.Lazy.get(Lazy.java:115) ~[spring-data-commons-2.7.16.jar:2.7.16] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.7.16.jar:2.7.16] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.7.16.jar:2.7.16] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.30.jar:5.3.30] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.30.jar:5.3.30] ... 44 common frames omitted Process finished with exit code 1

 

후.. Multi Module로 프로젝트를 진행하니까 설정 파일도 제각각 많아져서 뭔가 더 어려워진 느낌이다.. 


정말 큰 도움 받은 곳 .. 어디에서도 EnableJpsRepositories 어노테이션을 붙이라고는 안 했는데 여기서 알려주셨다 ㅠ

https://nantech.tistory.com/32

 

스프링 배치 에러 (The injection porint has the following annotations)

해당 프로젝트는 다중 모듈을 사용하고 있음 POM에도 관련 모듈이 잘 등록되어있고, 소스코드 상에서는 문제가 없었으나 어노테이션 오류 발생 *************************** APPLICATION FAILED TO START **********

nantech.tistory.com

 

728x90