26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
28
import org .springframework .beans .factory .BeanFactoryUtils ;
29
+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
29
30
import org .springframework .beans .factory .annotation .Autowired ;
30
- import org .springframework .beans .factory .annotation .Value ;
31
+ import org .springframework .beans .factory .config .BeanDefinition ;
32
+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
31
33
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
32
34
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
33
35
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
34
36
import org .springframework .boot .autoconfigure .condition .SpringBootCondition ;
37
+ import org .springframework .boot .bind .RelaxedPropertyResolver ;
35
38
import org .springframework .context .ApplicationContext ;
39
+ import org .springframework .context .EnvironmentAware ;
36
40
import org .springframework .context .annotation .Bean ;
37
41
import org .springframework .context .annotation .Condition ;
38
42
import org .springframework .context .annotation .ConditionContext ;
61
65
@ Configuration
62
66
@ ConditionalOnClass (EmbeddedDatabaseType .class /* Spring JDBC */ )
63
67
@ ConditionalOnMissingBean (DataSource .class )
64
- public class DataSourceAutoConfiguration {
68
+ public class DataSourceAutoConfiguration implements EnvironmentAware {
65
69
66
70
private static Log logger = LogFactory .getLog (DataSourceAutoConfiguration .class );
67
71
@@ -71,8 +75,66 @@ public class DataSourceAutoConfiguration {
71
75
@ Autowired
72
76
private ApplicationContext applicationContext ;
73
77
78
+ private RelaxedPropertyResolver environment ;
79
+
80
+ @ Override
81
+ public void setEnvironment (Environment environment ) {
82
+ this .environment = new RelaxedPropertyResolver (environment , "spring.database." );
83
+ }
84
+
85
+ @ PostConstruct
86
+ protected void initialize () throws Exception {
87
+ if (this .dataSource == null ) {
88
+ logger .debug ("No DataSource found so not initializing" );
89
+ return ;
90
+ }
91
+
92
+ String schema = this .environment .getProperty ("schema" );
93
+ if (schema == null ) {
94
+ schema = "classpath*:schema-"
95
+ + this .environment .getProperty ("platform" , "all" ) + ".sql" ;
96
+ }
97
+
98
+ List <Resource > resources = new ArrayList <Resource >();
99
+ for (String schemaLocation : StringUtils .commaDelimitedListToStringArray (schema )) {
100
+ resources .addAll (Arrays .asList (this .applicationContext
101
+ .getResources (schemaLocation )));
102
+ }
103
+
104
+ boolean exists = false ;
105
+ ResourceDatabasePopulator populator = new ResourceDatabasePopulator ();
106
+ for (Resource resource : resources ) {
107
+ if (resource .exists ()) {
108
+ exists = true ;
109
+ populator .addScript (resource );
110
+ populator .setContinueOnError (true );
111
+ }
112
+ }
113
+
114
+ if (exists ) {
115
+ DatabasePopulatorUtils .execute (populator , this .dataSource );
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Determines if the {@code dataSource} being used by Spring was created from
121
+ * {@link EmbeddedDataSourceConfiguration}.
122
+ * @return true if the data source was auto-configured.
123
+ */
124
+ public static boolean containsAutoConfiguredDataSource (
125
+ ConfigurableListableBeanFactory beanFactory ) {
126
+ try {
127
+ BeanDefinition beanDefinition = beanFactory .getBeanDefinition ("dataSource" );
128
+ return EmbeddedDataSourceConfiguration .class .getName ().equals (
129
+ beanDefinition .getFactoryBeanName ());
130
+ }
131
+ catch (NoSuchBeanDefinitionException ex ) {
132
+ return false ;
133
+ }
134
+ }
135
+
74
136
@ Conditional (DataSourceAutoConfiguration .EmbeddedDatabaseCondition .class )
75
- @ Import (EmbeddedDatabaseConfiguration .class )
137
+ @ Import (EmbeddedDataSourceConfiguration .class )
76
138
protected static class EmbeddedConfiguration {
77
139
}
78
140
@@ -82,7 +144,7 @@ protected static class TomcatConfiguration {
82
144
}
83
145
84
146
@ Conditional (DataSourceAutoConfiguration .BasicDatabaseCondition .class )
85
- @ Import (BasicDataSourceConfiguration .class )
147
+ @ Import (CommonsDataSourceConfiguration .class )
86
148
protected static class DbcpConfiguration {
87
149
}
88
150
@@ -107,35 +169,6 @@ public NamedParameterJdbcOperations namedParameterJdbcTemplate() {
107
169
108
170
}
109
171
110
- @ Value ("${spring.database.schema:classpath*:schema-${spring.database.platform:all}.sql}" )
111
- private String schemaLocations = "" ;
112
-
113
- @ PostConstruct
114
- protected void initialize () throws Exception {
115
- if (this .dataSource == null ) {
116
- logger .debug ("No DataSource found so not initializing" );
117
- return ;
118
- }
119
- ResourceDatabasePopulator populator = new ResourceDatabasePopulator ();
120
- boolean exists = false ;
121
- List <Resource > resources = new ArrayList <Resource >();
122
- for (String location : StringUtils
123
- .commaDelimitedListToStringArray (this .schemaLocations )) {
124
- resources
125
- .addAll (Arrays .asList (this .applicationContext .getResources (location )));
126
- }
127
- for (Resource resource : resources ) {
128
- if (resource .exists ()) {
129
- exists = true ;
130
- populator .addScript (resource );
131
- populator .setContinueOnError (true );
132
- }
133
- }
134
- if (exists ) {
135
- DatabasePopulatorUtils .execute (populator , this .dataSource );
136
- }
137
- }
138
-
139
172
static abstract class NonEmbeddedDatabaseCondition extends SpringBootCondition {
140
173
141
174
protected abstract String getDataSourceClassName ();
@@ -149,12 +182,13 @@ public Outcome getMatchOutcome(ConditionContext context,
149
182
+ " DataSource class not found" );
150
183
}
151
184
152
- String driverClassName = getDriverClassName (context .getEnvironment ());
185
+ String driverClassName = getDriverClassName (context .getEnvironment (),
186
+ context .getClassLoader ());
153
187
if (driverClassName == null ) {
154
188
return Outcome .noMatch ("no database driver" );
155
189
}
156
190
157
- String url = getUrl (context .getEnvironment ());
191
+ String url = getUrl (context .getEnvironment (), context . getClassLoader () );
158
192
if (url == null ) {
159
193
return Outcome .noMatch ("no database URL" );
160
194
}
@@ -166,24 +200,21 @@ public Outcome getMatchOutcome(ConditionContext context,
166
200
return Outcome .match ("missing database driver " + driverClassName );
167
201
}
168
202
169
- private String getDriverClassName (Environment environment ) {
203
+ private String getDriverClassName (Environment environment , ClassLoader classLoader ) {
170
204
String driverClassName = environment == null ? null : environment
171
205
.getProperty ("spring.database.driverClassName" );
172
206
if (driverClassName == null ) {
173
- driverClassName = EmbeddedDatabaseConfiguration
174
- .getEmbeddedDatabaseDriverClass (EmbeddedDatabaseConfiguration
175
- .getEmbeddedDatabaseType ());
207
+ driverClassName = EmbeddedDatabaseConnection .get (classLoader )
208
+ .getDriverClassName ();
176
209
}
177
210
return driverClassName ;
178
211
}
179
212
180
- private String getUrl (Environment environment ) {
213
+ private String getUrl (Environment environment , ClassLoader classLoader ) {
181
214
String url = (environment == null ? null : environment
182
215
.getProperty ("spring.database.url" ));
183
216
if (url == null ) {
184
- url = EmbeddedDatabaseConfiguration
185
- .getEmbeddedDatabaseUrl (EmbeddedDatabaseConfiguration
186
- .getEmbeddedDatabaseType ());
217
+ url = EmbeddedDatabaseConnection .get (classLoader ).getUrl ();
187
218
}
188
219
return url ;
189
220
}
@@ -229,8 +260,8 @@ public Outcome getMatchOutcome(ConditionContext context,
229
260
if (anyMatches (context , metadata , this .tomcatCondition , this .dbcpCondition )) {
230
261
return Outcome .noMatch ("existing non-embedded database detected" );
231
262
}
232
- EmbeddedDatabaseType type = EmbeddedDatabaseConfiguration
233
- . getEmbeddedDatabaseType ();
263
+ EmbeddedDatabaseType type = EmbeddedDatabaseConnection . get (
264
+ context . getClassLoader ()). getType ();
234
265
if (type == null ) {
235
266
return Outcome .noMatch ("no embedded database detected" );
236
267
}
0 commit comments