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.jdbc; 018 019import javax.sql.DataSource; 020 021import org.apache.commons.dbcp2.BasicDataSource; 022 023import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 024import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 025import org.springframework.boot.context.properties.ConfigurationProperties; 026import org.springframework.context.annotation.Bean; 027import org.springframework.context.annotation.Configuration; 028import org.springframework.context.annotation.Primary; 029 030/** 031 * Example configuration for configuring a configurable secondary {@link DataSource} while 032 * keeping the auto-configuration defaults for the primary one. 033 * 034 * @author Stephane Nicoll 035 */ 036public class SimpleTwoDataSourcesExample { 037 038 /** 039 * A simple configuration that exposes two data sources. 040 */ 041 @Configuration 042 static class SimpleDataSourcesConfiguration { 043 044 // tag::configuration[] 045 @Bean 046 @Primary 047 @ConfigurationProperties("app.datasource.foo") 048 public DataSourceProperties fooDataSourceProperties() { 049 return new DataSourceProperties(); 050 } 051 052 @Bean 053 @Primary 054 @ConfigurationProperties("app.datasource.foo") 055 public DataSource fooDataSource() { 056 return fooDataSourceProperties().initializeDataSourceBuilder().build(); 057 } 058 059 @Bean 060 @ConfigurationProperties("app.datasource.bar") 061 public BasicDataSource barDataSource() { 062 return (BasicDataSource) DataSourceBuilder.create() 063 .type(BasicDataSource.class).build(); 064 } 065 // end::configuration[] 066 067 } 068 069}