gpt4 book ai didi

spring-boot - Spring Boot 应用程序忽略 hikaricp 配置

转载 作者:行者123 更新时间:2023-12-05 02:09:25 34 4
gpt4 key购买 nike

我是 stackoverflow 的新手,但在这里阅读了大量帖子,现在卡住了。我的 application.properties 已读取,但配置 hikaricp 的部分被忽略/无效。

我读了https://www.javadevjournal.com/spring-boot/spring-boot-hikari/并按照这些步骤进行操作,仍然取得成功。

pom.xml

    <dependencies>  
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.10.Final</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
<version>2.2.2.RELEASE</version>
</dependency>

应用程序属性

    spring.cache.jcache.config=classpath:ehcache.xml
spring.datasource.jdbc-url=jdbc:postgresql://VOC-APP202-db:5432/voice-app
spring.datasource.username=vocapp202
spring.datasource.password=******
srping.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connectionTimeout=1000
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.maxLifetime=60000
spring.datasource.hikari.connectionTestQuery=SELECT * FROM table where id=1
spring.datasource.hikari.minimumIdle=1
spring.datasource.hikari.maximumPoolSize=5
spring.datasource.hikari.poolName=voiceapp-db-pool
spring.datasource.hikari.autoCommit=false

BlacklistApplication.class:

    package de.mycompany.voice.blacklist_ng;  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
@EnableCaching
@Configuration
public class BlacklistngApplication {

public static void main(String[] args) {
SpringApplication.run(BlacklistngApplication.class, args);
}

}

配置类:

    @Configuration
@ConfigurationProperties("spring.datasource")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"de.firsttelecom.voice.blacklist_ng.repository.vocapp202"}
)
public class VocApp202DbConfig extends HikariConfig {

@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
return new HikariDataSource(this);
}

我错过了什么?

最佳答案

spring.datasource.hikari.*(以及 spring.datasource.url)属性仅在您使用 Spring Boot DataSource 自动配置时才有效。为了实现它,您需要删除这个 bean:

 @Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
return new HikariDataSource(this);
}

Spring Boot 将使用所有这些属性自动为您创建它。您可以查看 DataSourceAutoConfiguration.class 了解更多信息。

如果由于某种原因,你不能删除这个 bean(例如,你有另一组数据源,你需要创建你的手动数据源以将其标记为 @Primary),你可以使用“原始”属性来配置 hikari。因此,您应该通过删除 hikari. 部分来修改您的属性,而不是删除 dataSource() 方法:

...
spring.datasource.jdbc-url=jdbc:postgresql://VOC-APP202-db:5432/voice-app
...
spring.datasource.connectionTimeout=1000
spring.datasource.idleTimeout=30000
spring.datasource.maxLifetime=60000
spring.datasource.connectionTestQuery=SELECT * FROM table where id=1
spring.datasource.minimumIdle=1
spring.datasource.maximumPoolSize=5
spring.datasource.poolName=voiceapp-db-pool
spring.datasource.autoCommit=false

jdbc-url 也是 hikari 特定的属性,这就是它现在可以工作的原因。

总结一下:没有 hikari.jdbc-url 的属性用于手动创建的数据源 bean,以及带有 hikari. 的属性>url 用于 Spring Boot 数据源自动配置。

关于spring-boot - Spring Boot 应用程序忽略 hikaricp 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894368/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com