Ho creato con successo un'applicazione di avvio a molla che utilizza il database incorporato di H2 in memoria. Vorrei ora cambiare questo in una versione basata su file che persisterà.Come configurare spring-boot per utilizzare il database H2 basato su file
Ho provato solo cambiando lo spring.datasource * immobili a mio file application.properties e simile a questo:.
spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=test spring.datasource.password=test spring.datasource.driverClassName=org.h2.Driver
Sembra avvio primavera appena ignora queste impostazioni, perché solo inizia come segue:
o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
mio pom.xml contiene le seguenti dipendenze che possono essere rilevanti per questo post:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
La mia comprensione dalla documentazione e un numero di post è che la configurazione dovrebbe funzionare, ma senza fortuna per me. Proprio per evitare alcuni degli errori di base che ho provato e controllato i seguenti:
- mie proprietà di applicazione è nella classspath:
- ho cercato di escludere la configurazione automatica in annotazione @EnableAutoConfiguration
- ho ha provato a iniettare un bean DataSource con combinazioni di annotazioni @Primary, @ConfigurationProperties (prefix = "spring.datasource") e imposta le proprietà a livello di codice con DataSourceBuilder. Ciò fa sì che altri errori relativi al tipo siano nulli.
Sembra che manchi un concetto chiave o qualcosa del genere. Qualcuno può aiutare.
UPDATE 1: Estrarre dal mio rapporto di configurazione automatica:
Positive matches:
-----------------
DataSourceAutoConfiguration matched
- @ConditionalOnClass classes found: javax.sql.DataSource,org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)
DataSourceAutoConfiguration.DataSourceInitializerConfiguration matched
- @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.EmbeddedConfiguration matched
- embedded database H2 detected (DataSourceAutoConfiguration.EmbeddedDataSourceCondition)
- @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration matched
- existing auto database detected (DataSourceAutoConfiguration.DataSourceAvailableCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#jdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#namedParameterJdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceTransactionManagerAutoConfiguration matched
- @ConditionalOnClass classes found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfiguration matched
- @ConditionalOnMissingBean (types: org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; SearchStrategy: all) found no beans (OnBeanCondition)
H2ConsoleAutoConfiguration matched
- @ConditionalOnClass classes found: org.h2.server.web.WebServlet (OnClassCondition)
- found web application StandardServletEnvironment (OnWebApplicationCondition)
- matched (OnPropertyCondition)
HibernateJpaAutoConfiguration matched
- @ConditionalOnClass classes found: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean,org.springframework.transaction.annotation.EnableTransactionManagement,javax.persistence.EntityManager (OnClassCondition)
- found HibernateEntityManager class (HibernateJpaAutoConfiguration.HibernateEntityManagerCondition)
Negative matches:
-----------------
DataSourceAutoConfiguration.NonEmbeddedConfiguration did not match
- missing supported DataSource (DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition)
`
UPDATE 2: Attuatore aggiunto e guardò endpoint/configprops. Ciò che è interessante qui è che la mia configurazione è stata presa e il database esiste ma quando l'applicazione viene eseguita non usa questa fonte dati.
"spring.datasource.CONFIGURATION_PROPERTIES":
{"prefix":"spring.datasource",
"properties":{
"schema":null,
"data":null,
"xa":{"dataSourceClassName":null,
"properties":{}
},
"type":null,
"separator":";",
"url":"jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE",
"platform":"all",
"continueOnError":false,
"jndiName":null,
"sqlScriptEncoding":null,
"password":"******",
"name":"testdb",
"driverClassName":"org.h2.Driver",
"initialize":true,
"username":"test"
}
}
grazie ma la configurazione crea effettivamente un'istanza di database in esecuzione con l'url corretto (come configurato, vedi UPDATE 2 sopra). Il problema è che l'applicazione non la sta usando.Sembra che stia usando l'EmbeddedDatabase di default – bitboy
hai aggiunto spring-boot-starter-jdbc in pom.xml? qui è un progetto di esempio: https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-flyway, flyway per la migrazione e h2database per il database embed dalla modalità file. – lenicliu
Aggiunta di spring-boot-starter-jdbc a pom.xml ha funzionato! Grazie. Sarebbe bello sapere perché questo l'ha risolto. – bitboy