简体   繁体   中英

Spring boot datasource lazy initialization

@Lazy annotation not working on datasource configuration. This datasource autowired into prototype scoped bean, but datasource initialize on startup eager. In stack tracei see call from TomcatServletWebServerFactory .

@Configuration
@Lazy
public class MsSqlMppvConfig {
@Bean
@ConfigurationProperties("spring.mppvdatasource")
public DataSourceProperties mppvDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Lazy
@Qualifier("mppvdatasource")
@ConfigurationProperties("spring.mppvdatasource.hikari")
public DataSource mppvDataSource() {
    return mppvDataSourceProperties().initializeDataSourceBuilder().build();
}


@Bean(name = "tm_mppvdatasource")
@Autowired
DataSourceTransactionManager tm(@Qualifier("mppvdatasource") DataSource datasource) {
    return new DataSourceTransactionManager(datasource);
}}

 @Autowired
    @Qualifier("mppvdatasource")
    @Lazy
    DataSource mppvDs;

Maybe problem in @Qualifier annotation?

Had the same issue when importing spring-boot-starter-jdbc . Had to change it to just use spring-jdbc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM