java 如何在 Spring 中将 jpa 属性加载到数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30310988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:53:08  来源:igfitidea点击:

how can i load jpa properties to datasource in Spring?

javaspringjpaspring-bootspring-data-jpa

提问by jscherman

I am using Spring boot Data JPA and right now, i have this:

我正在使用 Spring boot Data JPA,现在,我有这个:

@Configuration
@PropertySource("classpath:persistence.properties")
@EnableTransactionManagement
public class PersistenceConfiguration {

    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(this.dataSource());
        entityManager.setPackagesToScan(new String[] {"com.example.movies.domain"});
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManager.setJpaVendorAdapter(vendorAdapter);
        entityManager.setJpaProperties(this.properties());
        return entityManager;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    private Properties properties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.ddl-auto", this.env.getProperty("spring.jpa.hibernate.ddl-auto"));
        properties.setProperty("hibernate.dialect", this.env.getProperty("spring.jpa.hibernate.dialect"));
        properties.setProperty("hibernate.show_sql", this.env.getProperty("spring.jpa.show-sql"));
        return properties;

    }

}

And my persistence.properties

还有我的persistence.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=myPassword

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=false

What i would to know is if there exists any way to load these JpaProperties automatically. I want that spring build it because right now, if i add new jpa property in persistence.properties, then it wouldn't be noticed that change until i put that property in Properties object. So, do you know if is that possible? Regards!

我想知道的是是否有任何方法可以自动加载这些 JpaProperties。我希望 spring 构建它,因为现在,如果我在persistence.properties 中添加新的 jpa 属性,那么在我将该属性放入 Properties 对象之前不会注意到该更改。那么,你知道这可能吗?问候!

采纳答案by K. Siva Prasad Reddy

As M. Denium suggested you don't have to configure all these beans by yourself, SpringBoot will do that for you if you configure properties in application.properties.

由于 M. Denium 建议您不必自己配置所有这些 bean,如果您在 application.properties 中配置属性,SpringBoot 会为您完成。

And if you want to have separate datasource/jpa properties for testing then use environment profiles. You can create application-dev.properties, application-uat.properties, application-prod.properties to configure respective environment settings and activate the desired profile.

如果您想拥有单独的数据源/jpa 属性进行测试,请使用环境配置文件。您可以创建 application-dev.properties、application-uat.properties、application-prod.properties 来配置相应的环境设置并激活所需的配置文件。

Take a look at the SpringBoot supported properties list at http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#common-application-properties

http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#common-application-properties查看 SpringBoot 支持的属性列表

You can configure JPA properties as follows:

您可以按如下方式配置 JPA 属性:

JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)

JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)

spring.jpa.properties.*= # properties to set on the JPA connection
spring.jpa.open-in-view=true
spring.jpa.show-sql=true
spring.jpa.database-platform=
spring.jpa.database=
spring.jpa.generate-ddl=false # ignored by Hibernate, might be useful for other vendors
spring.jpa.hibernate.naming-strategy= # naming classname
spring.jpa.hibernate.ddl-auto= # defaults to create-drop for embedded dbs
spring.data.jpa.repositories.enabled=true # if spring data repository support is enabled

If you follow this naming convention you don't have to configure beans yourself.

如果您遵循此命名约定,则不必自己配置 bean。