BeanCreationException Error creating bean with name dataSource defined in ServletContext resource

The error message is as follows:

2020-04-02 15:25:15.549 WARN 4360 — [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-04-02 15:25:15.549 INFO 4360 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2020-04-02 15:25:15.554 INFO 4360 — [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]

Solution:

Method 1:

Add @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) to the startup class

package com.example.securitycore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class SecurityCoreApplication {

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

}

Method 2:

Add configuration in application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/security_demo?characterEncoding=utf-8
spring.datasource.data-username=root
spring .datasource.data-password=root

Read More:

Spring을 이용해서  Mybatis를 띄우면서 마주친 오류들과 해결 방법에 대하여 정리해두었다.

<driverClassName, Log4jdbc 오류>

Property 'driverClassName' threw exception; nested exception is java.lang.NoClassDefFoundError: Unable to find Log4j2 as default logging library.

driverClassName을 룰에맞게 잘 설정했는데도 해당 에러가 나오면

Log4j2가 없어서인것.. 파일 몇개를 추가 설정해서 에러를 해결

참고 : http://blog.naver.com/PostView.nhn?blogId=allkanet72&logNo=220926784590&parentCategoryNo=&categoryNo=28&viewDate=&isShowPopularPosts=false&from=postView

https://gaegoeb.tistory.com/1

https://min-it.tistory.com/6- classpath, mybatis설정 설명 잘되어있음

<sqlSessionFactory 빈 생성시 오류>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/transaction/TransactionFactory

org/apache/ibatis/transaction/TransactionFactory를 보면

Mybatis와 Mybatis-spring의 버전이 맞지않아서 생기는 오류인 것을 알 수 있다.

http://mvnrepository.com 에서 mybatis-spring버전과 맞는 mybatis 의존성을 추가했다. (pom.xml에)

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency>

참고 :  https://developer-kylee.tistory.com/5

<root-context.xml 오류>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/classpath*:/mybatis-config.xml]

root-context.xml에 경로를 제대로 작성하지 않았을때 나오는 오류이다.

<!-- Mybatis 연동 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> <property name="mapperLocations" value="classpath*:/mappers/*Mapper.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean>

이렇게 바꿔주었다. 실제 Mapper.xml이 들어있는 경로를 맞춰줄것.

<@Autowired 주입 시 오류>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'boardServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.firstspring.web.mapper.BoardMapper com.firstspring.web.service.impl.BoardServiceImpl.mapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.firstspring.web.mapper.BoardMapper] 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)} No matching bean of type [com.firstspring.web.mapper.BoardMapper] 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)}

@Autowired  의존성 주입이 제대로 되지 않아서 나오는 오류이다.

Mapper를 제대로 받아오지 못하는 것 같아서 서치해본 결과..

<mybatis-spring:scan base-package="com.firstspring.web.mapper"/>

root-context.xml에 위의 mybatis base-package를 추가해주니 해결되었다.

환경설정이 실제 로직 구현보다 더 어려움을 절실히 깨닫는.. 어제오늘이였다^_ㅠ