【问题标题】:how can i load jpa properties to datasource in Spring?如何在 Spring 中将 jpa 属性加载到数据源?
【发布时间】:2015-07-30 10:00:47
【问题描述】:

我正在使用 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对象之前不会注意到这种变化。那么,你知道这是否可能吗?问候!

【问题讨论】:

  • 使用框架而不是围绕框架工作。基本上,您可以删除所有配置,因为 Spring Boot 会自动为您配置所有这些。基本上你想要的默认工作,但因为你已经解决了它不再工作的所有问题。
  • 不要那样做,你应该使用相同的机制而不是绕过它。
  • 不,您也可以使用 Spring Boot 进行测试(在 Spring Boot 参考指南中有说明)。你不需要不同的配置,如果你开始走这条路,你最终会在你的测试用例中使用 Spring Boot,那么你的测试用例的价值是什么?
  • 您阅读过 Spring Boot 参考指南吗?这解释了如何重用现有配置来编写测试、集成测试或 Web 相关测试。
  • 但是您在其他帖子中没有使用@SpringApplicationConfiguration,而是@ContextConfiguration,所以它没有使用任何东西。此外,您还明确配置了 JPA,它也提供了。恕我直言,您试图通过解决问题而不是解决问题来解决问题。使用@SpringApplicationConfiguration 并将其指向您的应用程序类。那么它应该一切正常。

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


【解决方案1】:

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

如果您想使用单独的数据源/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。

【讨论】:

  • 谢谢。但是你知道为什么我在运行测试时会得到 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] 吗?我已经有指向 DAOConfiguration.class 的 SpringApplicationConfiguration 注释(它有 JPA 实现 bean)并且仍然失败。我不明白...
  • 它是否在主应用程序中工作并且仅在 JUnit 测试中失败?如果是这样,您可以发布您的 JUnit 代码 sn-ps 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 2018-06-03
相关资源
最近更新 更多