You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何手动创建可自动加载application.yaml中全部JPA属性的Spring JPA Entity Manager以支持多数据源

解决手动创建EntityManager时自动加载JPA配置的问题

你说得对,手动逐个配置所有JPA属性确实繁琐,尤其是还要兼顾Spring的默认值。其实Spring已经提供了现成的工具,能帮你自动加载application.yaml里的所有JPA配置,不用手动硬编码每一项。

方法1:利用JpaProperties自动绑定全局JPA配置

Spring的JpaProperties类会自动读取application.yaml中以spring.jpa为前缀的所有配置,包括它自带的默认值(比如你提到的SpringPhysicalNamingStrategy)。你只需要把这个类注入到配置类中,再把它的属性转换成Map即可:

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;
import java.util.Map;

@Configuration
public class MultiDataSourceConfig {

    private final JpaProperties jpaProperties;

    // 构造注入自动加载好的JPA属性
    public MultiDataSourceConfig(JpaProperties jpaProperties) {
        this.jpaProperties = jpaProperties;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManager(EntityManagerFactoryBuilder builder, DataSource secondDataSource) {
        // 获取application.yaml里的所有JPA属性(含Spring默认值)
        Map<String, Object> props = jpaProperties.getHibernateProperties(new HibernateJpaVendorAdapter());
        
        // 如果需要给当前数据源单独覆盖某些属性,在这里添加即可
        // props.put("hibernate.show_sql", "true");
        
        return builder.dataSource(secondDataSource)
                .packages(MyEntity.class)
                .properties(props)
                .build();
    }
}

方法2:为多数据源配置专属JPA属性

如果你的第二个数据源需要独立的JPA配置(比如在application.yaml里写spring.jpa.second.*),可以自定义属性类,用@ConfigurationProperties绑定专属前缀:

首先在application.yaml中添加第二个数据源的JPA配置:

spring:
  jpa:
    # 全局JPA配置
    hibernate:
      ddl-auto: update
    # 第二个数据源的专属配置
    second:
      hibernate:
        ddl-auto: none
        show_sql: true

然后创建自定义属性类:

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.jpa.second")
public class SecondJpaProperties extends JpaProperties {
    // 无需额外代码,继承JpaProperties即可自动绑定前缀下的所有属性
}

最后在配置类中使用这个自定义属性类:

@Configuration
@EnableConfigurationProperties(SecondJpaProperties.class)
public class MultiDataSourceConfig {

    private final SecondJpaProperties secondJpaProperties;

    public MultiDataSourceConfig(SecondJpaProperties secondJpaProperties) {
        this.secondJpaProperties = secondJpaProperties;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManager(EntityManagerFactoryBuilder builder, DataSource secondDataSource) {
        Map<String, Object> props = secondJpaProperties.getHibernateProperties(new HibernateJpaVendorAdapter());
        
        return builder.dataSource(secondDataSource)
                .packages(MyEntity.class)
                .properties(props)
                .build();
    }
}

这样不管是全局JPA配置还是数据源专属配置,都能自动加载,不用再手动逐个设置属性了。

内容的提问来源于stack exchange,提问作者His

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:37:39