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.docs.jdbc; 018 019import com.zaxxer.hikari.HikariDataSource; 020import org.apache.commons.dbcp2.BasicDataSource; 021 022import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 023import org.springframework.boot.context.properties.ConfigurationProperties; 024import org.springframework.context.annotation.Bean; 025import org.springframework.context.annotation.Configuration; 026import org.springframework.context.annotation.Primary; 027 028/** 029 * Example configuration for configuring two data sources with what Spring Boot does in 030 * auto-configuration. 031 * 032 * @author Stephane Nicoll 033 */ 034public class CompleteTwoDataSourcesExample { 035 036 /** 037 * A complete configuration that exposes two data sources. 038 */ 039 @Configuration 040 static class CompleteDataSourcesConfiguration { 041 042 // tag::configuration[] 043 @Bean 044 @Primary 045 @ConfigurationProperties("app.datasource.first") 046 public DataSourceProperties firstDataSourceProperties() { 047 return new DataSourceProperties(); 048 } 049 050 @Bean 051 @Primary 052 @ConfigurationProperties("app.datasource.first.configuration") 053 public HikariDataSource firstDataSource() { 054 return firstDataSourceProperties().initializeDataSourceBuilder() 055 .type(HikariDataSource.class).build(); 056 } 057 058 @Bean 059 @ConfigurationProperties("app.datasource.second") 060 public DataSourceProperties secondDataSourceProperties() { 061 return new DataSourceProperties(); 062 } 063 064 @Bean 065 @ConfigurationProperties("app.datasource.second.configuration") 066 public BasicDataSource secondDataSource() { 067 return secondDataSourceProperties().initializeDataSourceBuilder() 068 .type(BasicDataSource.class).build(); 069 } 070 // end::configuration[] 071 072 } 073 074}