• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

서버 실행 실패

24.04.04 17:12 작성 조회수 146

0

> Task :LibraryAppApplication.main() FAILED

. ____ _

/\\ / ___'_ __ (_)_ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v2.7.6)

2024-04-04 17:09:40.815 INFO 12468 --- [ main] c.g.libraryapp.LibraryAppApplication : Starting LibraryAppApplication using Java 17.0.9 on KONG with PID 12468 (C:\Project\project\library-app\build\classes\java\main started by jinju in C:\Project\project\library-app)

2024-04-04 17:09:40.819 INFO 12468 --- [ main] c.g.libraryapp.LibraryAppApplication : No active profile set, falling back to 1 default profile: "default"

2024-04-04 17:09:41.422 INFO 12468 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2024-04-04 17:09:41.474 WARN 12468 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'userRepository' defined in com.group.libraryapp.domain.user.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot register bean definition [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in com.group.libraryapp.domain.user.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration] for bean 'userRepository': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=userConfiguration; factoryMethodName=userRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/group/libraryapp/config/UserConfiguration.class]] bound.

2024-04-04 17:09:41.505 INFO 12468 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2024-04-04 17:09:41.521 ERROR 12468 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************

APPLICATION FAILED TO START

***************************

Description:

The bean 'userRepository', defined in com.group.libraryapp.domain.user.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in class path resource [com/group/libraryapp/config/UserConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Execution failed for task ':LibraryAppApplication.main()'.

> Process 'command 'C:/Program Files/Java/jdk-17/bin/java.exe'' finished with non-zero exit value 1

 

찾아본 결과
이 문제는 Spring 애플리케이션의 빈(bean) 정의 중에 중복이 발생하여 발생한 것으로 보입니다. 에러 메시지에 따르면 userRepository 빈이 이미 UserConfiguration 클래스에서 정의되어 있으며, 또한 Spring Data JPA가 자체적으로 해당 빈을 생성하려고 시도하고 있는 것으로 보인다고 합니다.

혹시 UserConfiguration 부분에 변경이 적용 안된게 있어서 그런걸까요?


저 bean 부분을 지워도 .UserServiceV1 때문에 컴파일 에러가 발생하더라구요,.,..

답변 2

·

답변을 작성해보세요.

1

안녕하세요!!!

 

AI 인턴이 알려준 것처럼

spring.main.allow-bean-definition-overriding=true

이 옵션을 적용하기 보다는,

 

중복된 Bean을 제거해주시는게 좋아요!! 예를 들어, 스크린샷을 찍어주신 코드는 강의에서 @Configuration@Bean 을 설명드리기 위한 예시 코드로, 제공되는 <완성본 코드>를 보시면 해당 코드는 제거되어 있어요!

 

아마 금방 해결 하셨을 것 같네요! 😊 👍

감사합니다! 🙏

1

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.04.04

안녕하세요, 인프런 AI 인턴입니다.

wnsqhr1230님의 서버 실행 실패 문제는 스프링 부트 애플리케이션에서 같은 이름의 빈(bean)이 중복으로 정의되어 있는 문제로 보입니다. 서버 실행 시 userRepository 관련 중복이 발생하여 해당 문제로 인해 애플리케이션 실행에 실패한 것으로 나타납니다. 이러한 문제는 일반적으로 @Configuration 어노테이션이 적용된 Java Config 클래스 내에서 특정 빈을 수동으로 설정하였고, 동시에 Spring Data JPA가 해당 리포지토리 인터페이스를 스캔하여 자동으로 빈을 등록하려 할 때 발생합니다.

해당 문제의 경우, Spring Data JPA가 自动生成的注解을 통해 userRepository 빈을 자동으로 생성하려 하지만, 이미 UserConfiguration 클래스에서 수동으로 같은 이름의 빈을 생성하려 하기 때문에 충돌 발생합니다.

이와 유사한 문제 해결 방법에 대해 설명한 과거 질문들이 있습니다:

  • 질문 “26강 코딩 후 컨파일 시 오류 문의드립니다 !”에서는 비슷한 문제로, UserRepository를 두 번 등록하는 것이 문제였습니다. 여기서 지식공유자는 UserConfiguration 클래스를 제거하거나 수정하여 문제를 해결하는 방법에 대해 설명합니다.
  • 또 다른 질문 “서버 실행 실패”에서는 기존 메소드 명을 제대로 지키지 않아 발생한 문제로, 메소드명을 올바르게 수정하여 해결한 사례를 살펴볼 수 있습니다.

님의 경우, UserConfiguration에서 userRepository를 수동으로 등록하고 같은 이름으로 자동 등록을 시도하기 때문에 발생한 문제로 추정됩니다. 해결을 위해 다음 조치를 고려할 수 있습니다:

  1. UserConfiguration 클래스에서 userRepository 관련 빈 설정을 제거합니다. 이렇게 하면 Spring Data JPA가 UserRepository 인터페이스를 자동으로 스캔하여 빈으로 등록할 수 있습니다.
  2. 그럼에도 문제가 지속된다면, spring.main.allow-bean-definition-overriding 속성을 application.properties 또는 application.yml 설정 파일에 true로 설정하여 빈 재정의를 허용할 수 있습니다. 그러나 이 방법은 기존 빈을 덮어쓰게 되므로 주의해야 합니다.
spring.main.allow-bean-definition-overriding=true

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

wnsqhr1230님의 프로필

wnsqhr1230

2024.04.04

감사합니당