Java 在 Spring Boot 中获取对当前活动数据源的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43142703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a reference to currently active dataSource in Spring Boot
提问by developer10
I want to implement db data init via DataSourceInitializer
.
我想通过DataSourceInitializer
.
I have these as methods just below my Spring Boot main method, but it seems that it doesn't get executed at all (I tried with intentional removal of characters just to trigger an error which would confirm the execution. Nothing happened.):
我在 Spring Boot main 方法下面有这些方法,但它似乎根本没有被执行(我试图故意删除字符只是为了触发一个错误来确认执行。什么也没发生。):
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource getDataSource() {
// i was hoping this was going to pull my current datasource, as
// defined in application.properties
return DataSourceBuilder
.create()
.build();
}
@Bean
public DataSourceInitializer dataSourceInitializer() {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("/data/init/initData.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
// the call to the above method
dataSourceInitializer.setDataSource(getDataSource());
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
UPDATE: This question was aimed at getting a reference to the dataSource in use. This question explains how to init the data in a very simple way: DataSourceInitializer is not working on Spring boot 1.2
更新:此问题旨在获取对正在使用的数据源的引用。这个问题以非常简单的方式解释了如何初始化数据: DataSourceInitializer is not working on Spring boot 1.2
采纳答案by Essex Boy
If you have a datasource already created it will be in the spring container, so:
如果您已经创建了一个数据源,它将在 spring 容器中,因此:
@Autowired
DataSource dataSource;
Should do it.
应该做。
回答by alfcope
You are saying you have those method below your application main method and you are not autowiring the datasource, so you are creating an instance directly and so that not using the properties. You need to use the singleton object created by Spring. In order to do that you have two possibilities:
您是说您的应用程序主方法下方有这些方法,并且您没有自动装配数据源,因此您直接创建了一个实例,因此不使用这些属性。您需要使用 Spring 创建的单例对象。为此,您有两种可能性:
First option, and the one you should use, is declare a config class to create your beans:
第一个选项,也是您应该使用的选项,是声明一个配置类来创建您的 bean:
@Configuration
public class DatasourceConfig
{
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource getDataSource() {
// i was hoping this was going to pull my current datasource, as
// defined in application.properties
return DataSourceBuilder
.create()
.build();
}
@Bean
public DataSourceInitializer dataSourceInitializer() {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("/data/init/initData.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
// the call to the above method
dataSourceInitializer.setDataSource(getDataSource());
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
}
Using @Configuration
, even calling the method directly, as Configuration classes are subclassed at startup-time with CGLIB
, you are getting the object created by Spring.
使用@Configuration
,甚至直接调用该方法,因为 Configuration 类在启动时使用 进行子类化CGLIB
,您将获得 Spring 创建的对象。
Further information about how Java-based configuration works internally
Second option is auto-wire the datasource in the second method:
第二个选项是在第二种方法中自动连接数据源:
@Bean
@Autowired
public DataSourceInitializer dataSourceInitializer(DataSource myDatasource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("/data/init/initData.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(myDatasource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}