如何在Spring中将jpa属性加载到datasource?

时间:2015-05-18 19:03:38

标签: java spring jpa spring-boot spring-data-jpa

我正在使用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;

    }

}

我的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

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

1 个答案:

答案 0 :(得分:5)

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

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

查看http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#common-application-properties

上的SpringBoot支持的属性列表

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

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

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