Commit c6175073 authored by Dave Syer's avatar Dave Syer

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
parent af825fa4
...@@ -26,8 +26,8 @@ import javax.sql.DataSource; ...@@ -26,8 +26,8 @@ import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
...@@ -48,7 +48,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized ...@@ -48,7 +48,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
private static Log logger = LogFactory.getLog(DataSourceInitializer.class); private static Log logger = LogFactory.getLog(DataSourceInitializer.class);
@Autowired @Autowired
private ApplicationContext applicationContext; private ConfigurableApplicationContext applicationContext;
@Autowired(required = false) @Autowired(required = false)
private DataSource dataSource; private DataSource dataSource;
...@@ -75,8 +75,14 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized ...@@ -75,8 +75,14 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
List<Resource> scripts = getScripts(this.properties.getSchema(), "schema"); List<Resource> scripts = getScripts(this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) { if (!scripts.isEmpty()) {
runScripts(scripts); runScripts(scripts);
this.applicationContext.publishEvent(new DataSourceInitializedEvent( try {
this.dataSource)); this.applicationContext.publishEvent(new DataSourceInitializedEvent(
this.dataSource));
}
catch (IllegalStateException e) {
logger.warn("Could not send event to complete DataSource initialization ("
+ e.getMessage() + ")");
}
} }
} }
......
...@@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor; ...@@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.Ordered;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
/** /**
...@@ -35,7 +36,14 @@ import org.springframework.core.type.AnnotationMetadata; ...@@ -35,7 +36,14 @@ import org.springframework.core.type.AnnotationMetadata;
* @author Dave Syer * @author Dave Syer
* @since 1.1.2 * @since 1.1.2
*/ */
class DataSourceInitializerPostProcessor implements BeanPostProcessor { class DataSourceInitializerPostProcessor implements BeanPostProcessor, Ordered {
private int order = Ordered.HIGHEST_PRECEDENCE;
@Override
public int getOrder() {
return this.order;
}
@Autowired @Autowired
private BeanFactory beanFactory; private BeanFactory beanFactory;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment