[英]How to create Custom DataSource in Spring Boot + Spring Data
I want to create Custom DataSource programmatically. 我想以编程方式创建Custom DataSource。 This is because I want to fetch password from a secure storage at runtime, instead of hardcoding it in application.yaml.
这是因为我想在运行时从安全存储中获取密码,而不是在application.yaml中对其进行硬编码。
I have a custom datasource - myapp.datasource.password which has the value of mydb.password.key. 我有一个自定义数据源 - myapp.datasource.password,其值为mydb.password.key。 Using ApplicationListener I am loading secrets and setting the value of myapp.datasource.password using mydb.password.key as key.
使用ApplicationListener我正在加载机密并使用mydb.password.key作为键设置myapp.datasource.password的值。
myapp:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&characterEncoding=utf8
username: myuser
password: mydb.password.key
Now, HibernateJpaAutoConfiguration is trying to connect to DB using mydb.password.key as password and failing at the time of startup. 现在,HibernateJpaAutoConfiguration正在尝试使用mydb.password.key作为密码连接到数据库,并在启动时失败。
I tried to exclude DB auto configuration classes 我试图排除数据库自动配置类
@SpringBootApplication
(exclude={
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
But it throws this exception 但它抛出了这个例外
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'entityManagerFactory'的bean可用
How to resolve this? 怎么解决这个? Any help is greatly appreciated!
任何帮助是极大的赞赏!
Thanks 谢谢
No need to exclude the built in autoconfiguration. 无需排除内置的自动配置。
In you application.yml 在你的application.yml
myapp:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&characterEncoding=utf8
username: myuser
# make sure you exclude password from here
And in your configuration: 在您的配置中:
// all the other datasource properties will be applied from "myapp.datasource" except password because you excluded it
@ConfigurationProperties(prefix = "myapp.datasource")
@Bean
@Primary // this will override the datasource autoconfiguration and use your own everywhere
public DataSource dataSource() {
String password = retrieveMyPasswordSecurely();
return DataSourceBuilder
.create()
.password(password)
.build();
}
You need to provide an Entity Manager Factory bean. 您需要提供Entity Manager Factory bean。 You can follow the convention of creating a bean with the name entityManagerFactory, assuming that you don't already have one.
您可以遵循创建名为entityManagerFactory的bean的约定,假设您还没有。 There's a few examples in the Spring documentation here and here .
这里和这里的Spring文档中有一些例子。
Assuming you've got a data source, you might be able to get away with as little as this: 假设你有一个数据源,你可以尽可能少地逃脱:
@Configuration
public class Database {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("org.yourcompany.jpaentityroot");
factory.setDataSource(dataSource);
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
return txManager;
}
@Resource private DataSource dataSource;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.