java 为什么Sping Boot 忽略我的h2数据库在application.properties中的spring. spring.url?

nwo49xxi  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(202)

我有一个Sping Boot 应用程序,它创建了一个h2数据库并加载了一些测试数据,然后我想用h2控制台查看这些数据。
以下是我的application.properties:

  1. spring.h2.console.enabled=true
  2. spring.h2.console.path=/h2-console
  3. spring.datasource.url=jdbc:h2:file:D:/ProgrammingStuff/wiktionaries/wiktdb
  4. spring.datasource.driverClassName=org.h2.Driver
  5. spring.datasource.username=sa
  6. spring.datasource.password=
  7. spring.jpa.hibernate.ddl-auto=create-drop

字符串
如果我注解掉spring. boot.url并且Sping Boot 使用默认的数据库路径(jdbc:h2:dbc:testdb),我可以打开h2控制台,连接到db并查看数据。但是如果我像上面那样指定URL,Sping Boot 控制台仍然显示行2023-12-29T10:23:56.665+01:00 INFO 9904 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'。并且我不能使用给定的数据库URL连接到它,只能使用jdbc:h2:mem:testdb URL。为什么Sping Boot 会忽略我给出的JDBC URL?
application.properties也应该在正确的路径:src/main/resources/application.properties x1c 0d1x
其他可能相关的文件:
pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>3.2.1</version>
  10. <relativePath /> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.pux12</groupId>
  13. <artifactId>dictionarycreator</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>dictionarycreator</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>21</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-jdbc</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-devtools</artifactId>
  32. <version>3.2.1</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.h2database</groupId>
  36. <artifactId>h2</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.json</groupId>
  45. <artifactId>json</artifactId>
  46. <version>20231013</version>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. <plugin>
  56. <groupId>org.jsonschema2pojo</groupId>
  57. <artifactId>jsonschema2pojo-maven-plugin</artifactId>
  58. <version>1.2.1</version>
  59. <configuration>
  60. <sourceDirectory>${basedir}/src/main/resources/wiktionary</sourceDirectory>
  61. <targetPackage>com.example.types</targetPackage>
  62. </configuration>
  63. <executions>
  64. <execution>
  65. <goals>
  66. <goal>generate</goal>
  67. </goals>
  68. </execution>
  69. </executions>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. </project>


SpringJdbcConfig.java

  1. package com.pux12.dictionarycreator;
  2. import javax.sql.DataSource;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  7. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  8. @Configuration
  9. @ComponentScan("com.pux12.dictionarycreator")
  10. public class SpringJdbcConfig {
  11. @Bean
  12. public DataSource dataSource() {
  13. return new EmbeddedDatabaseBuilder()
  14. .setType(EmbeddedDatabaseType.H2)
  15. .addScript("classpath:jdbc/schema.sql")
  16. .addScript("classpath:jdbc/data.sql").build();
  17. }
  18. }


Spring Boot 日志:

  1. :: Spring Boot :: (v3.2.1)
  2. 2023-12-29T10:49:54.327+01:00 INFO 14272 --- [ restartedMain] c.p.d.DictionarycreatorApplication : Starting DictionarycreatorApplication using Java 21.0.1 with PID 14272 (C:\Users\hanne\Documents\Programme\dictionarycreator\target\classes started by hanne in C:\Users\hanne\Documents\Programme\dictionarycreator)
  3. 2023-12-29T10:49:54.332+01:00 INFO 14272 --- [ restartedMain] c.p.d.DictionarycreatorApplication : No active profile set, falling back to 1 default profile: "default"
  4. 2023-12-29T10:49:54.418+01:00 INFO 14272 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
  5. 2023-12-29T10:49:54.418+01:00 INFO 14272 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
  6. 2023-12-29T10:49:56.425+01:00 INFO 14272 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with
  7. port 8080 (http)
  8. 2023-12-29T10:49:56.456+01:00 INFO 14272 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  9. 2023-12-29T10:49:56.458+01:00 INFO 14272 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.17]
  10. 2023-12-29T10:49:56.555+01:00 INFO 14272 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  11. 2023-12-29T10:49:56.558+01:00 INFO 14272 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2137 ms
  12. 2023-12-29T10:49:56.589+01:00 INFO 14272 --- [ restartedMain] 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'
  13. 2023-12-29T10:49:56.860+01:00 INFO 14272 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at
  14. '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
  15. 2023-12-29T10:49:57.775+01:00 INFO 14272 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
  16. 2023-12-29T10:49:57.834+01:00 INFO 14272 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
  17. 2023-12-29T10:49:57.853+01:00 INFO 14272 --- [ restartedMain] c.p.d.DictionarycreatorApplication : Started DictionarycreatorApplication in 4.008 seconds (process running for 4.663)


Windows 10,Sping Boot 3.2.1,VS Code with Java extensions

i86rm4rw

i86rm4rw1#

当你手动创建Datasource bean时,它不会遵循配置的spring.datasource设置。特别是,当你使用.setType(EmbeddedDatabaseType.H2)时,它将使用默认配置生成代理,除非在创建过程中显式重写。
有关ConnectionProperties的其他详细信息,可以浏览org.springframework.jdbc.datasource.embedded.H2EmbeddedDatabaseConfigurer中H2使用的默认配置。
要更新URL中的数据库名称,可以按如下方式修改Datasource bean:

  1. @Bean
  2. public DataSource dataSource() {
  3. return new EmbeddedDatabaseBuilder()
  4. .setType(EmbeddedDatabaseType.H2)
  5. .setName("custom_db")
  6. .addScripts("classpath:jdbc/schema.sql", "classpath:jdbc/data.sql")
  7. .build();
  8. }

字符串

编辑

H2类型中使用EmbeddedDatabaseBuilder时,它默认为内存数据库,到目前为止,还没有一种直接的方法来自定义此行为。可能值得考虑联系开发团队询问添加此功能的可能性。作为替代方案,如果您更喜欢使用文件模式,可以通过配置来实现相同的功能。
以下是一些示例配置:

  1. spring:
  2. h2:
  3. console:
  4. enabled: true
  5. datasource:
  6. url: jdbc:h2:file:C:/data/sample # Windows only
  7. username: sa
  8. password: sa
  9. sql:
  10. init:
  11. schema-locations: classpath:jdbc/schema.sql
  12. data-locations: classpath:jdbc/data.sql

展开查看全部

相关问题