본문 바로가기
web

[spring을 spring 답게] spring 세팅

by java개발자 2020. 3. 27.

application.properties와 DatabaseConfiguration.class를 구현하였음에도

아래와 같은 에러가 발생하는 이유는...

main class가 위치한 package의 하위 package들을 스캔하기 때문이다.

엉뚱한 곳에 DatabaseConfiguration.class를 작성하여서 scan 자체가 안된 것이다.

spring boot에서 가장 기본적인 내용임에도 불구하고, 간과하고 다른 곳에서 원인을 찾고 있었다. (*.properties)

역시나 reference에는 명시되어 있다.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-structuring-your-code


3.2.2. Locating the Main Application Class

We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @SpringBootApplication annotated class is used to search for @Entity items. Using a root package also allows component scan to apply only on your project.


custom.spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
custom.spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
custom.spring.datasource.hikari.username=root
custom.spring.datasource.hikari.password=root
custom.spring.datasource.hikari.connection-test-query=SELECT 1
package board.configuration;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@PropertySource("classpath:/application.properties")
public class DatabaseConfiguration {
	@Bean
	@ConfigurationProperties(prefix = "custom.spring.datasource.hikari")
	public HikariConfig hikariConfig() {
		return new HikariConfig();
	}
	@Bean
	public DataSource dataSource() {
		DataSource dataSource = new HikariDataSource(hikariConfig());
		return dataSource;
	}
}

error msg

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

 

 


정상로그

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-03-27 21:13:08.342  INFO 13292 --- [  restartedMain] board.BoardApplication                   : Starting BoardApplication on DESKTOP-G5807HG with PID 13292 (F:\workspace2020\2020-java\03.SpringBootStarting\board\bin\main started by ysh in F:\workspace2020\2020-java\03.SpringBootStarting\board)
2020-03-27 21:13:08.347  INFO 13292 --- [  restartedMain] board.BoardApplication                   : No active profile set, falling back to default profiles: default
2020-03-27 21:13:08.391  INFO 13292 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-03-27 21:13:08.391  INFO 13292 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-03-27 21:13:09.055  WARN 13292 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[board]' package. Please check your configuration.
2020-03-27 21:13:10.006  INFO 13292 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-27 21:13:10.035  INFO 13292 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-27 21:13:10.035  INFO 13292 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-03-27 21:13:10.045  INFO 13292 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre-9.0.4\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre-9.0.4/bin/server;C:/Program Files/Java/jre-9.0.4/bin;C:\Program Files\Java\jdk-9.0.4\bin;F:\java_apps\apache-ant-1.10.7\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\nodejs\;C:\Users\ysh\AppData\Local\Atlassian\SourceTree\git_local\bin;F:\apps\graphviz-2.38\release\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Java\jdk-9.0.4\bin;F:\java_apps\apache-ant-1.10.7\bin;C:\Program Files\heroku\bin;C:\Users\ysh\AppData\Local\Microsoft\WindowsApps;C:\Users\ysh\Downloads\poppler-0.68.0_x86\poppler-0.68.0\bin;C:\Users\ysh\Anaconda3\;C:\Users\ysh\Anaconda3\Scripts;C:\Users\ysh\Anaconda3\Library\bin;C:\tizen-studio\tools\ide\bin;;C:\Program Files (x86)\ESTsoft\ALSee\x64;C:\Users\ysh\Desktop;;.]
2020-03-27 21:13:10.166  INFO 13292 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-27 21:13:10.167  INFO 13292 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1776 ms
2020-03-27 21:13:10.237  INFO 13292 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-03-27 21:13:10.480  INFO 13292 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-03-27 21:13:10.690  INFO 13292 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-27 21:13:11.023  INFO 13292 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-03-27 21:13:11.078  INFO 13292 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-27 21:13:11.083  INFO 13292 --- [  restartedMain] board.BoardApplication                   : Started BoardApplication in 3.015 seconds (JVM running for 3.568)

에러로그

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-03-27 21:13:37.929  INFO 30648 --- [  restartedMain] board.BoardApplication                   : Starting BoardApplication on DESKTOP-G5807HG with PID 30648 (F:\workspace2020\2020-java\03.SpringBootStarting\board\bin\main started by ysh in F:\workspace2020\2020-java\03.SpringBootStarting\board)
2020-03-27 21:13:37.932  INFO 30648 --- [  restartedMain] board.BoardApplication                   : No active profile set, falling back to default profiles: default
2020-03-27 21:13:37.979  INFO 30648 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-03-27 21:13:37.979  INFO 30648 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-03-27 21:13:38.606  WARN 30648 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[board]' package. Please check your configuration.
2020-03-27 21:13:39.613  INFO 30648 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-27 21:13:39.641  INFO 30648 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-27 21:13:39.641  INFO 30648 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-03-27 21:13:39.653  INFO 30648 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre-9.0.4\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre-9.0.4/bin/server;C:/Program Files/Java/jre-9.0.4/bin;C:\Program Files\Java\jdk-9.0.4\bin;F:\java_apps\apache-ant-1.10.7\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\nodejs\;C:\Users\ysh\AppData\Local\Atlassian\SourceTree\git_local\bin;F:\apps\graphviz-2.38\release\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Java\jdk-9.0.4\bin;F:\java_apps\apache-ant-1.10.7\bin;C:\Program Files\heroku\bin;C:\Users\ysh\AppData\Local\Microsoft\WindowsApps;C:\Users\ysh\Downloads\poppler-0.68.0_x86\poppler-0.68.0\bin;C:\Users\ysh\Anaconda3\;C:\Users\ysh\Anaconda3\Scripts;C:\Users\ysh\Anaconda3\Library\bin;C:\tizen-studio\tools\ide\bin;;C:\Program Files (x86)\ESTsoft\ALSee\x64;C:\Users\ysh\Desktop;;.]
2020-03-27 21:13:39.789  INFO 30648 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-27 21:13:39.789  INFO 30648 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1810 ms
2020-03-27 21:13:39.998  INFO 30648 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-27 21:13:40.158  WARN 30648 --- [  restartedMain] 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-03-27 21:13:40.158  INFO 30648 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-03-27 21:13:40.161  INFO 30648 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (file:/C:/Users/ysh/.gradle/caches/modules-2/files-2.1/org.apache.tomcat.embed/tomcat-embed-core/9.0.13/20c90a060e1e497e0c1398f59c058279a8ae203d/tomcat-embed-core-9.0.13.jar) to field java.io.ObjectStreamClass$Caches.localDescs
WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoaderBase
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-03-27 21:13:40.183  INFO 30648 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-27 21:13:40.185 ERROR 30648 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

'web' 카테고리의 다른 글

github actions  (0) 2021.04.27
graphql 기본 문법 정리  (0) 2020.07.03
react.js  (0) 2020.07.03
vue.js vs react.js  (0) 2020.06.24
Vue.js global error 처리  (0) 2020.03.13