我如何参数化 Spring 4 上的数据源属性?
how can i parameterize datasource properties on Spring 4?
我正在使用 Spring 4.16。我想参数化我的持久性数据。这是我现在的配置:
@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
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
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sarasa_db");
dataSource.setUsername("root");
dataSource.setPassword("mypassword");
return dataSource;
}
@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.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}
我想尽我所能对我的 application.properties 进行参数化。首先,我想放入数据源属性(正如我正在阅读的那样,spring 可能会自动构建我的数据源,但显然那只是使用 JdbcTemplate...):
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=mypassword
而且,如果可能的话,我在文档中找不到的所有特性
你知道我该怎么做吗?
编辑
这是我的 DAO 实现
@Configuration
@Import(PersistenceConfiguration.class)
public class DAOConfiguration {
@PersistenceContext
private EntityManager entityManager;
@Bean
public ClientDAO clientDAO() {
SimpleJpaRepository<Client, String> support = this.getSimpleJpaRepository(Client.class);
return new MySQLClientDAO(support);
}
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Description("Hibernate repository helper")
protected <T> SimpleJpaRepository<T, String> getSimpleJpaRepository(Class<T> domainClass) {
return new SimpleJpaRepository<T, String>(domainClass, this.entityManager);
}
}
你可以这样做:
首先在 Spring 配置中的某处定义 PropertySourcesPlaceholderConfigurer
bean:
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("application.properties"));
return ppc;
}
此配置假定 application.properties
文件位于 class 路径的根目录。
设置 属性 占位符配置器后,您可以访问数据库配置中的属性 class,如下所示:
@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {
@Value("${spring.datasource.url}")
private String jdbcUrl;
// ...
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(jdbcUrl);
// ...
}
}
如果您想要一种简单的方法来参数化所有属性,您应该看看 Spring Boot。它使用 application.properties
文件自动创建具有这些属性和许多其他内容的数据源。这可能是您在问题中提到的自动数据源创建。
我正在使用 Spring 4.16。我想参数化我的持久性数据。这是我现在的配置:
@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
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
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sarasa_db");
dataSource.setUsername("root");
dataSource.setPassword("mypassword");
return dataSource;
}
@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.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}
我想尽我所能对我的 application.properties 进行参数化。首先,我想放入数据源属性(正如我正在阅读的那样,spring 可能会自动构建我的数据源,但显然那只是使用 JdbcTemplate...):
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=mypassword
而且,如果可能的话,我在文档中找不到的所有特性
你知道我该怎么做吗?
编辑
这是我的 DAO 实现
@Configuration
@Import(PersistenceConfiguration.class)
public class DAOConfiguration {
@PersistenceContext
private EntityManager entityManager;
@Bean
public ClientDAO clientDAO() {
SimpleJpaRepository<Client, String> support = this.getSimpleJpaRepository(Client.class);
return new MySQLClientDAO(support);
}
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Description("Hibernate repository helper")
protected <T> SimpleJpaRepository<T, String> getSimpleJpaRepository(Class<T> domainClass) {
return new SimpleJpaRepository<T, String>(domainClass, this.entityManager);
}
}
你可以这样做:
首先在 Spring 配置中的某处定义 PropertySourcesPlaceholderConfigurer
bean:
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("application.properties"));
return ppc;
}
此配置假定 application.properties
文件位于 class 路径的根目录。
设置 属性 占位符配置器后,您可以访问数据库配置中的属性 class,如下所示:
@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {
@Value("${spring.datasource.url}")
private String jdbcUrl;
// ...
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(jdbcUrl);
// ...
}
}
如果您想要一种简单的方法来参数化所有属性,您应该看看 Spring Boot。它使用 application.properties
文件自动创建具有这些属性和许多其他内容的数据源。这可能是您在问题中提到的自动数据源创建。