java - 尽管数据库关闭,但启动 SpringBoot 服务器
问题描述
我有这个连接到 PostgreSQL 数据库的 SpringBoot 应用程序。即使数据库已关闭,我也想启动服务器,但continue-on-error=true
不起作用。当我模拟数据库关闭(密码错误)时,我得到了Error creating bean with name 'countryRepository'
错误,即使存储库是延迟加载的。
我认为这是因为默认的@EnableJpaRepositories。有谁知道如何懒惰地初始化它?
通过添加 EntityManagerFactory、EntityManager、TransactionManager、TransactionTemplate 的自定义、延迟初始化的实现来解决
应用程序属性
# Database Properties
spring.datasource.url=jdbc:postgresql://localhost:5432/world-db
spring.datasource.username=world
spring.datasource.password=world123F
spring.datasource.hikari.initializationFailTimeout=0
# Not working...
spring.datasource.continue-on-error=true
spring.datasource.initialization-mode=never
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL92Dialect
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.minimum-idle=0
spring.datasource.hikari.initialization-fail-timeout=-1
# Hibernate Properties
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL92Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.h2.console.enabled=true
AppConfig.java
@Lazy
@Configuration
@ComponentScan(basePackages = "com.flairstech.workshop")
public class AppConfig {
@Bean
public CountryLanguage getCountryLanguage(){
return new CountryLanguage();
}
@Bean
public Country getCountry(){
return new Country();
}
}
解决方案
推荐阅读
- python - 数据框中的重复行添加新的自定义列
- java - 超出内存限制:使用 Java Scanner 时
- .net - dotnetdependensee 对路径的访问被拒绝
- python - 使用 mypy 对方法进行后置条件
- uri - URI 方案的 URI 方案
- reactjs - 如何在 localhost (NextJS) 上运行 web 应用程序的 nrwl/nx 生产版本
- flutter - 如何在 Flutter 中保持状态变化
- c# - 通过 .NET SDK 创建链接的自托管集成运行时
- rust - 为什么在引用时,int lterals 会继续堆?
- php - 如何使用谷歌距离矩阵获取两个位置之间的距离