不同的PersistenceConfig.java用于开发和生产

时间:2014-10-23 13:32:44

标签: java spring spring-data

下面是我用于开发的PersistenceConfig.java文件。但是在生产中,由于我在那里使用postgresql,因此看起来会有很大不同。处理这个问题的好方法是什么?所以我不会被迫进入一个if-else地狱。

import ...;

@Configuration
@EnableJpaRepositories
public class PersistenceConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(dataSource());
        factory.setPackagesToScan(packagesToScan);
        factory.setJpaVendorAdapter(jpaVendorAdapter());
//      factory.setJpaProperties(additionalProperties());
        return factory;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(true);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        return hibernateJpaVendorAdapter;
    }

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

1 个答案:

答案 0 :(得分:1)

使用Spring Profiles来区分环境。我会配置生产的所有东西,只是覆盖开发。还将一些属性移动到属性文件。

@Configuration
@EnableJpaRepositories
@PropertySource("classpath:application.properties")
public class PersistenceConfig {

    @Autowired
    private Environment env;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource() {
        // Production configuration
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(dataSource);
        factory.setPackagesToScan(packagesToScan);
        factory.setJpaVendorAdapter(jpaVendorAdapter());
//      factory.setJpaProperties(additionalProperties());
        return factory;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(env.getProperty("hibernate.show-sql", Boolean.class, false));
        hibernateJpaVendorAdapter.setGenerateDdl(env.getProperty("hibernate.generate-schema", Boolean.class, false));
        hibernateJpaVendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect" );
        return hibernateJpaVendorAdapter;
    }

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

    @Configuration
    @Profile("dev")
    public static class DevPersistenceConfig {
        @Bean
        public DataSource dataSource() {
            return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();      
        }
    }
}

这样的事情应该有用。默认配置为生产,当配置文件dev处于活动状态时(可以使用spring.profiles.active属性设置为环境变量),数据源将被DevPersistenceConfig类中的数据源覆盖

可以使用application.properties文件中的条目(或您喜欢的任何名称)设置方言。