Spring Boot 애플리케이션을 개발할 때, DB를 쓰지 않을 때는 @EnableAutoConfiguration(exclude='') 를 이용하여 초기화 할 때, DB 연결이 되지 않도록 한다.


@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, RabbitAutoConfiguration.class,
FreeMarkerAutoConfiguration.class})
@ImportResource("classpath:application-context.xml")
//@SpringBootApplication
public class Application {

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



이렇게 아무 생각없이 사용하다가, DB 관련 설정을 추가하여 개발하려고 테스트하다
다음과 같이 NoSuchBeanDefinitionException [javax.sql.DataSource] 이 발생할 수 있다. 
(JPA 연동 설명 : https://mhdevelopment.wordpress.com/2014/02/09/bootstrap-an-application-with-spring-boot-part1-command-line/)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}



이럴 때는 DataSource관련 클래스를 @EnableAutoConfiguration(exclude='')에서 제외시킨다.
 

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { RabbitAutoConfiguration.class,
        SecurityAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class})
@ImportResource("classpath:application-context.xml")
//@SpringBootApplication
public class Application {

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

에러 없이 DB 연결되고, DataSource에 적절한 객체가 바인딩될 수 있다. 


https://mhdevelopment.wordpress.com/2014/02/09/bootstrap-an-application-with-spring-boot-part1-command-line/


Posted by '김용환'
,