Spring

SpringBoot-어떤 조건이 참 값을 가질 때“ 'entityManagerFactory'라는 이름으로 빈 생성 오류…”를 방지하는 방법

기록만이살길 2021. 3. 5. 07:04
반응형

SpringBoot-어떤 조건이 참 값을 가질 때“ 'entityManagerFactory'라는 이름으로 빈 생성 오류…”를 방지하는 방법

1. 질문(문제점):

SpringBoot 애플리케이션 (버전 1.5.22.RELEASE)이 있으며 MariaDB 데이터베이스를 데이터 소스로 사용하여 시작시 캐시를 채우기위한 데이터를 가져옵니다. 데이터는 엔티티 컬렉션을 사용하는 JPA 저장소에서 가져옵니다. 모든 데이터 소스 매개 변수가 application.properties파일에 있습니다.

이때 캐시 데이터 소스를 데이터베이스 대신 REST 서비스로 전환해야합니다. 에 값이있는 새 속성이있는 true경우 DB가 꺼지기 때문에 REST 서비스를 사용해야합니다. 그렇지 않으면 (의 새 속성과 함께 false) REST 서비스를 사용할 수 없으므로 데이터베이스를 사용해야합니다.

속성이 true있고 데이터베이스를 사용할 수없는 상태 에서 내 응용 프로그램을 테스트하려고 하면 응용 프로그램에서 다음 오류가 발생하고 응용 프로그램이 시작되지 않습니다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]

데이터베이스를 사용할 수 없을 때이 오류를 방지하고 응용 프로그램 시작을 계속할 수있는 방법이 있습니까?

미리 감사드립니다. 귀하의 도움과 피드백에 감사드립니다.

2. 해결방안:

예, 아래 코드를 사용하여 데이터베이스 자동 구성을 비활성화 할 수 있습니다.

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class Application {

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

그런 다음 시작시 데이터베이스에 수동으로 연결할있습니다 . 데이터베이스의 가용성을 감지하고 캐시 데이터를 가져 오거나 연결 오류가 발생한 다음 백업 서비스로 전환하십시오.

65745708
반응형