001/*
002 * Copyright 2012-2018 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.data.jdbc;
018
019import java.util.Optional;
020
021import org.springframework.boot.autoconfigure.AutoConfigureAfter;
022import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
027import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.context.annotation.Import;
030import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
031import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
032import org.springframework.data.jdbc.repository.config.JdbcConfiguration;
033import org.springframework.data.jdbc.repository.config.JdbcRepositoryConfigExtension;
034import org.springframework.data.relational.core.conversion.RelationalConverter;
035import org.springframework.data.relational.core.mapping.NamingStrategy;
036import org.springframework.data.relational.core.mapping.RelationalMappingContext;
037import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
038
039/**
040 * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JDBC Repositories.
041 * <p>
042 * Once in effect, the auto-configuration is the equivalent of enabling JDBC repositories
043 * using the {@link EnableJdbcRepositories} annotation and providing a
044 * {@link JdbcConfiguration} subclass.
045 *
046 * @author Andy Wilkinson
047 * @since 2.1.0
048 * @see EnableJdbcRepositories
049 */
050@Configuration
051@ConditionalOnBean(NamedParameterJdbcOperations.class)
052@ConditionalOnClass({ NamedParameterJdbcOperations.class, JdbcConfiguration.class })
053@ConditionalOnProperty(prefix = "spring.data.jdbc.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
054@AutoConfigureAfter(JdbcTemplateAutoConfiguration.class)
055public class JdbcRepositoriesAutoConfiguration {
056
057        @Configuration
058        @ConditionalOnMissingBean(JdbcRepositoryConfigExtension.class)
059        @Import(JdbcRepositoriesAutoConfigureRegistrar.class)
060        static class JdbcRepositoriesConfiguration {
061
062        }
063
064        @Configuration
065        @ConditionalOnMissingBean(JdbcConfiguration.class)
066        static class SpringBootJdbcConfiguration extends JdbcConfiguration {
067
068                // Remove these public methods when they are made
069                // public in Spring Data
070                @Override
071                public JdbcCustomConversions jdbcCustomConversions() {
072                        return super.jdbcCustomConversions();
073                }
074
075                @Override
076                public RelationalMappingContext jdbcMappingContext(
077                                Optional<NamingStrategy> namingStrategy) {
078                        return super.jdbcMappingContext(namingStrategy);
079                }
080
081                @Override
082                public RelationalConverter relationalConverter(
083                                RelationalMappingContext mappingContext) {
084                        return super.relationalConverter(mappingContext);
085                }
086
087        }
088
089}