001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.jdbc; 018 019import javax.sql.DataSource; 020 021import org.springframework.boot.autoconfigure.AutoConfigureAfter; 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 026import org.springframework.boot.context.properties.EnableConfigurationProperties; 027import org.springframework.context.annotation.Bean; 028import org.springframework.context.annotation.Configuration; 029import org.springframework.context.annotation.Import; 030import org.springframework.context.annotation.Primary; 031import org.springframework.jdbc.core.JdbcOperations; 032import org.springframework.jdbc.core.JdbcTemplate; 033import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; 034import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 035 036/** 037 * {@link EnableAutoConfiguration Auto-configuration} for {@link JdbcTemplate} and 038 * {@link NamedParameterJdbcTemplate}. 039 * 040 * @author Dave Syer 041 * @author Phillip Webb 042 * @author Stephane Nicoll 043 * @author Kazuki Shimizu 044 * @since 1.4.0 045 */ 046@Configuration 047@ConditionalOnClass({ DataSource.class, JdbcTemplate.class }) 048@ConditionalOnSingleCandidate(DataSource.class) 049@AutoConfigureAfter(DataSourceAutoConfiguration.class) 050@EnableConfigurationProperties(JdbcProperties.class) 051public class JdbcTemplateAutoConfiguration { 052 053 @Configuration 054 static class JdbcTemplateConfiguration { 055 056 private final DataSource dataSource; 057 058 private final JdbcProperties properties; 059 060 JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) { 061 this.dataSource = dataSource; 062 this.properties = properties; 063 } 064 065 @Bean 066 @Primary 067 @ConditionalOnMissingBean(JdbcOperations.class) 068 public JdbcTemplate jdbcTemplate() { 069 JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); 070 JdbcProperties.Template template = this.properties.getTemplate(); 071 jdbcTemplate.setFetchSize(template.getFetchSize()); 072 jdbcTemplate.setMaxRows(template.getMaxRows()); 073 if (template.getQueryTimeout() != null) { 074 jdbcTemplate 075 .setQueryTimeout((int) template.getQueryTimeout().getSeconds()); 076 } 077 return jdbcTemplate; 078 } 079 080 } 081 082 @Configuration 083 @Import(JdbcTemplateConfiguration.class) 084 static class NamedParameterJdbcTemplateConfiguration { 085 086 @Bean 087 @Primary 088 @ConditionalOnSingleCandidate(JdbcTemplate.class) 089 @ConditionalOnMissingBean(NamedParameterJdbcOperations.class) 090 public NamedParameterJdbcTemplate namedParameterJdbcTemplate( 091 JdbcTemplate jdbcTemplate) { 092 return new NamedParameterJdbcTemplate(jdbcTemplate); 093 } 094 095 } 096 097}