如何在Spring 4上参数化数据源属性?

时间:2021-12-09 03:13:43

I am using Spring 4.16. I want to parameterize my persistence data. This is my config right now:

我使用的是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;
    }

}

And i want to parameterize on my application.properties all things i can. First of all, i want to put in datasource properties (so as i was reading, is possible that spring builds my datasource automatically, but apparently that is only using JdbcTemplate...):

我想在我的application.properties上参数化所有我能做的事情。首先,我想放入数据源属性(因为我正在阅读,春天可能会自动构建我的数据源,但显然只使用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

And, if it's possible, all the Properties's properties, which i couldn't find nothing in doc

并且,如果可能的话,所有属性的属性,我在doc中找不到任何内容

Do you know how could i do it ?

你知道我怎么办?


EDIT

This is my DAO implementation

这是我的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);
    }

}

1 个解决方案

#1


You could do something like this:

你可以这样做:

First define PropertySourcesPlaceholderConfigurer bean somewhere in your Spring configuration:

首先在Spring配置中的某处定义PropertySourcesPlaceholderConfigurer bean:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setLocation(new ClassPathResource("application.properties"));
    return ppc;
}

This configuration assumes that application.properties file is placed at the root of your classpath.

此配置假定application.properties文件位于类路径的根目录中。

After setting up the property placeholder configurer you can access the properties in your database configuration class like so:

设置属性占位符配置器后,您可以访问数据库配置类中的属性,如下所示:

@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {

    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    // ...

    @Bean
    public DataSource dataSource() {
       DriverManagerDataSource dataSource = new DriverManagerDataSource();
       dataSource.setUrl(jdbcUrl);
       // ...
    }
}

If you want an easy way to parametrize all properties, you should take a look at Spring Boot. It uses the application.properties file to automatically create data source with those properties, and many other things. This is probably the automatic datasource creation you mentioned in your question.

如果您想要一种简单的方法来参数化所有属性,那么您应该看看Spring Boot。它使用application.properties文件自动创建具有这些属性的数据源以及许多其他内容。这可能是您在问题中提到的自动数据源创建。

#1


You could do something like this:

你可以这样做:

First define PropertySourcesPlaceholderConfigurer bean somewhere in your Spring configuration:

首先在Spring配置中的某处定义PropertySourcesPlaceholderConfigurer bean:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setLocation(new ClassPathResource("application.properties"));
    return ppc;
}

This configuration assumes that application.properties file is placed at the root of your classpath.

此配置假定application.properties文件位于类路径的根目录中。

After setting up the property placeholder configurer you can access the properties in your database configuration class like so:

设置属性占位符配置器后,您可以访问数据库配置类中的属性,如下所示:

@Configuration
@EnableTransactionManagement
public class PersistenceConfiguration {

    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    // ...

    @Bean
    public DataSource dataSource() {
       DriverManagerDataSource dataSource = new DriverManagerDataSource();
       dataSource.setUrl(jdbcUrl);
       // ...
    }
}

If you want an easy way to parametrize all properties, you should take a look at Spring Boot. It uses the application.properties file to automatically create data source with those properties, and many other things. This is probably the automatic datasource creation you mentioned in your question.

如果您想要一种简单的方法来参数化所有属性,那么您应该看看Spring Boot。它使用application.properties文件自动创建具有这些属性的数据源以及许多其他内容。这可能是您在问题中提到的自动数据源创建。