web-dev-qa-db-fra.com

Configuration de la base de données H2 via YAML - Spring Boot

J'ai une application de démarrage à printemps et je souhaite configurer ma base de données H2 avec YAML. Il se bloque chaque fois que je vais courir cela.

Mon fichier app.yml:

spring:
  database: h2
  console: true
  path: /h2

datasource:
  platform: h2
  url: jdbc:h2://localhost:8080/h2
  username: sa
  password: 1234
  driverClassName: org.h2.Driver

pom.xml

<dependencies>

    <!-- H2 Database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

....
</dependencies>

Je ne sais pas très bien utiliser Yaml, et j'ai besoin d'aide pour voir si mon YAML peut être réparé pour incorporer la base de données H2.

J'essaie de faire ceci: https://dzone.com/articles/integrate-h2-database-in-your-spring-boot-Applicat

Mais avec app.yml à la place de app.properties.

Merci d'avance!

Edit: voici mon crash,

Java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$ServletSessionConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3b764bce]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.Java:507) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.Java:404) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.Java:389) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.Java:447) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at Java.base/Java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.Java:1751) ~[na:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.Java:738) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.Java:679) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.Java:647) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.Java:1518) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.Java:509) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.Java:479) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.Java:600) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.Java:592) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.Java:1226) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.Java:905) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.Java:891) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.Java:877) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.Java:826) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:327) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at pw.restMaker.restMaker.RestMakerApplication.main(RestMakerApplication.Java:10) [classes/:na]
Caused by: Java.lang.NoClassDefFoundError: org/springframework/security/web/authentication/RememberMeServices
    at Java.base/Java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
    at Java.base/Java.lang.ClassLoader.defineClass(ClassLoader.Java:1007) ~[na:na]
    at Java.base/Java.security.SecureClassLoader.defineClass(SecureClassLoader.Java:174) ~[na:na]
    at Java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.Java:801) ~[na:na]
    at Java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.Java:699) ~[na:na]
    at Java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.Java:622) ~[na:na]
    at Java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.Java:580) ~[na:na]
    at Java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.Java:185) ~[na:na]
    at Java.base/Java.lang.ClassLoader.loadClass(ClassLoader.Java:496) ~[na:na]
    at Java.base/Java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at Java.base/Java.lang.Class.privateGetDeclaredMethods(Class.Java:3139) ~[na:na]
    at Java.base/Java.lang.Class.getDeclaredMethods(Class.Java:2266) ~[na:na]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.Java:489) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    ... 21 common frames omitted
Caused by: Java.lang.ClassNotFoundException: org.springframework.security.web.authentication.RememberMeServices
    at Java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.Java:582) ~[na:na]
    at Java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.Java:185) ~[na:na]
    at Java.base/Java.lang.ClassLoader.loadClass(ClassLoader.Java:496) ~[na:na]
    ... 34 common frames omitted
3
Compiler v2

Étant donné :

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

Quelque chose comme ça devrait fonctionner:

spring:
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: jdbc:h2:file:~/test
    username: sa
    password:
    driverClassName: org.h2.Driver

Le fichier doit être placé dans src/main/resources répertoire et nommé application.yml.

7
michalk