멀티 모듈에서 타 모듈의 Bean 등록 오류

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:

오류

 

 

멀티 모듈 환경에서 타 모듈의 Entity나 Service, Repository를 참조해서 쓰려고 하는데 Bean 등록이 안됐다;;

 

: Error creating bean with name 'userController': Lookup method resolution failed

 

Description: Parameter 0 of constructor in com.example.admin.user.test.UserService required a bean of type 'com.example.admin.user.test.UserRepository' that could not be found. Action: Consider defining a bean of type 'com.example.admin.user.test.UserRepository' in your configuration.

 

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:

 

뭐 이런 오류들이 계속 나왔다.

 

해결

 A 모듈에서 B 모듈을 불러오고 싶을 때

implementation(project(':B'))

 

이렇게 build.gradle에 의존성 추가해주고

 

import jakarta.persistence.Entity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackages = {"com.example.B.user.model","com.example.A.model"})
@ComponentScan(basePackages = {"com.example.B.user","com.example.A"})
@EnableJpaRepositories(basePackages = {"com.example.B","com.example.A"})
public class AApplication {

    public static void main(String[] args) {
        SpringApplication.run(AApplication.class, args);
    }

}

 

이런 식으로 EntityScan, ComponentScan EnableJpaRepositories 어노테이션 달아서 패키지 범위를 지정해주면 다른 모듈에 있는거 쓸 수 있었다!

 

주의점

basePackages 에 들어있는 것만 Bean으로 등록하는거라 자기 모듈의 패키지도 포함시켜줘야한다!