2019年8月18日 星期日

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

 解決方式:
加入以下紅字部分停用Spring Boot的資料庫自動配置
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class XXXApplication {
 public static void main(String[] args) {
  SpringApplication.run(XXXApplication.class, args);
 }
}

錯誤訊息
:: Spring Boot ::        (v2.1.7.RELEASE)
2019-08-18 19:59:10.049  INFO 18260 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-18 19:59:10.049  INFO 18260 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1388 ms
2019-08-18 19:59:10.109  WARN 18260 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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
2019-08-18 19:59:10.111  INFO 18260 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-08-18 19:59:10.122  INFO 18260 --- [           main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-18 19:59:10.128 ERROR 18260 --- [           main] 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).

2019年8月16日 星期五

Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11


如果要把專案改成Java 11,可以參考以下文章
Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11
https://altkomsoftware.pl/en/blog/spring-boot-migration-java/

Spring Boot 1.5不支援Java 11造成ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader

原因:
Spring Boot 1.5不支援Java 11
改用JDK 8後就正常

如果要把專案改成Java 11,可以參考以下文章
Spring Boot 1.5.x to 2.0.x Migration – first step of migration to Java 11
https://altkomsoftware.pl/en/blog/spring-boot-migration-java/

Exception in thread "main" java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getUrls(DefaultRestartInitializer.java:93)
 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getInitialUrls(DefaultRestartInitializer.java:56)
 at org.springframework.boot.devtools.restart.Restarter.<init>(Restarter.java:140)
 at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:546)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
 at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
 at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:292)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
 at com.example.demo.DemoJavaWebApplication.main(DemoJavaWebApplication.java:12)