在Spring Boot中获取对当前活动的dataSource的引用

时间:2017-03-31 14:05:32

标签: java spring spring-boot datasource

我想通过@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; } 实现db data init。

我将这些方法作为我的Spring Boot主方法下面的方法,但它似乎根本没有被执行(我试图故意删除字符只是为了触发一个错误,这将确认执行。没有发生任何事情。 ):

for(var i=0;i<bookmarks.length;i++)
{
var name=bookmarks[i].name;
var url=bookmarks[i].url;
bookmarksresults.innerHTML+='<div class="well">'+ 
                            '<h3>'+name+ 
                            '<a class="btn btn-default" target="_blank" 
                             href="'+url+'">Visit</a> '+
                            '<a onclick="deleteBookmark(\''+url+'\')" +
                            class="btn btn-danger" href="#">Delete</a> '+
                            '</h3>' +
                            '</div>';
}

更新:此问题旨在获取对正在使用的dataSource的引用。这个问题解释了如何以一种非常简单的方式初始化数据: DataSourceInitializer is not working on Spring boot 1.2

2 个答案:

答案 0 :(得分:18)

如果您已经创建了数据源,它将位于spring容器中,所以:

@Autowired
DataSource dataSource;

应该这样做。

答案 1 :(得分:0)

您说您在应用程序主方法下面有这些方法,并且您没有自动装配数据源,因此您直接创建实例,因此不使用属性。您需要使用Spring创建的单例对象。为了做到这一点,你有两种可能性:

第一个选项,你应该使用的选项,是声明一个配置类来创建你的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;
    }

}

使用@Configuration,甚至直接调用该方法,因为配置类在启动时使用CGLIB进行子类化,您将获得Spring创建的对象。

Further information about how Java-based configuration works internally

第二个选项是在第二种方法中自动连接数据源:

@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;
}