Skip to content

Commit c617507

Browse files
author
Dave Syer
committed
Be slightly more defensive in DataSource initialization
Adding an Order to the BeanPostProcessor and catching an exception are enough to get a simple web app with @EnableGlobalMethodSecurity and JDBC user details running. It actually doesn't solve an underlying problem in Spring Security, but I'll deal with that separately. See gh-1115
1 parent af825fa commit c617507

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.context.ApplicationContext;
3029
import org.springframework.context.ApplicationListener;
30+
import org.springframework.context.ConfigurableApplicationContext;
3131
import org.springframework.core.io.Resource;
3232
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
3333
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@@ -48,7 +48,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
4848
private static Log logger = LogFactory.getLog(DataSourceInitializer.class);
4949

5050
@Autowired
51-
private ApplicationContext applicationContext;
51+
private ConfigurableApplicationContext applicationContext;
5252

5353
@Autowired(required = false)
5454
private DataSource dataSource;
@@ -75,8 +75,14 @@ private void runSchemaScripts() {
7575
List<Resource> scripts = getScripts(this.properties.getSchema(), "schema");
7676
if (!scripts.isEmpty()) {
7777
runScripts(scripts);
78-
this.applicationContext.publishEvent(new DataSourceInitializedEvent(
79-
this.dataSource));
78+
try {
79+
this.applicationContext.publishEvent(new DataSourceInitializedEvent(
80+
this.dataSource));
81+
}
82+
catch (IllegalStateException e) {
83+
logger.warn("Could not send event to complete DataSource initialization ("
84+
+ e.getMessage() + ")");
85+
}
8086
}
8187
}
8288

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerPostProcessor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2727
import org.springframework.beans.factory.support.GenericBeanDefinition;
2828
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
29+
import org.springframework.core.Ordered;
2930
import org.springframework.core.type.AnnotationMetadata;
3031

3132
/**
@@ -35,7 +36,14 @@
3536
* @author Dave Syer
3637
* @since 1.1.2
3738
*/
38-
class DataSourceInitializerPostProcessor implements BeanPostProcessor {
39+
class DataSourceInitializerPostProcessor implements BeanPostProcessor, Ordered {
40+
41+
private int order = Ordered.HIGHEST_PRECEDENCE;
42+
43+
@Override
44+
public int getOrder() {
45+
return this.order;
46+
}
3947

4048
@Autowired
4149
private BeanFactory beanFactory;

0 commit comments

Comments
 (0)