001/* 002 * Copyright 2012-2016 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.context.annotation.Bean; 027import org.springframework.context.annotation.Configuration; 028import org.springframework.context.annotation.Primary; 029import org.springframework.jdbc.core.JdbcOperations; 030import org.springframework.jdbc.core.JdbcTemplate; 031import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; 032import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 033 034/** 035 * {@link EnableAutoConfiguration Auto-configuration} for {@link JdbcTemplate} and 036 * {@link NamedParameterJdbcTemplate}. 037 * 038 * @author Dave Syer 039 * @author Phillip Webb 040 * @author Stephane Nicoll 041 * @author Kazuki Shimizu 042 * @since 1.4.0 043 */ 044@Configuration 045@ConditionalOnClass({ DataSource.class, JdbcTemplate.class }) 046@ConditionalOnSingleCandidate(DataSource.class) 047@AutoConfigureAfter(DataSourceAutoConfiguration.class) 048public class JdbcTemplateAutoConfiguration { 049 050 private final DataSource dataSource; 051 052 public JdbcTemplateAutoConfiguration(DataSource dataSource) { 053 this.dataSource = dataSource; 054 } 055 056 @Bean 057 @Primary 058 @ConditionalOnMissingBean(JdbcOperations.class) 059 public JdbcTemplate jdbcTemplate() { 060 return new JdbcTemplate(this.dataSource); 061 } 062 063 @Bean 064 @Primary 065 @ConditionalOnMissingBean(NamedParameterJdbcOperations.class) 066 public NamedParameterJdbcTemplate namedParameterJdbcTemplate() { 067 return new NamedParameterJdbcTemplate(this.dataSource); 068 } 069 070}