如何将 jpa 属性加载到 Spring 中的数据源?
how can i load jpa properties to datasource in Spring?
我正在使用 Spring 启动数据 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 属性,那么在我将 属性 放入之前不会注意到该更改属性对象。那么,你知道这是否可能吗?问候!
正如 M. Denium 建议的那样,您不必自己配置所有这些 bean,如果您在 application.properties 中配置属性,SpringBoot 将为您完成。
如果您想要单独的 datasource/jpa 属性进行测试,请使用环境配置文件。您可以创建 application-dev.properties、application-uat.properties、application-prod.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。
我正在使用 Spring 启动数据 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 属性,那么在我将 属性 放入之前不会注意到该更改属性对象。那么,你知道这是否可能吗?问候!
正如 M. Denium 建议的那样,您不必自己配置所有这些 bean,如果您在 application.properties 中配置属性,SpringBoot 将为您完成。
如果您想要单独的 datasource/jpa 属性进行测试,请使用环境配置文件。您可以创建 application-dev.properties、application-uat.properties、application-prod.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。