
虽然我们的自动注入会爆红,但是我们还是可以直接使用的,具体原因我也不清楚。但是在源码中我们的JDBCTemplate有被加载到IOC容器中所以我们是可以直接使用@Autowired自动注入的。
源码:
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnMissingBean({JdbcOperations.class})
class JdbcTemplateConfiguration {
JdbcTemplateConfiguration() {
}
@Bean
@Primary
JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
Template template = properties.getTemplate();
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate.setQueryTimeout((int)template.getQueryTimeout().getSeconds());
}
return jdbcTemplate;
}
}
本文探讨了如何在Spring框架中,即使自动注入失败也能使用JDBCTemplate,通过源码解析,揭示了JdbcTemplate被注入到IOC容器的过程。重点在于配置类和@Bean注解的使用,以及模板参数的设置。
879

被折叠的 条评论
为什么被折叠?



