Remove checked exceptions from @PostConstructs

Update all @PostConstruct methods to ensure that they don't throw
checked exceptions. Required to allow deployment of Spring Boot
applications on Glassfish.

Fixes gh-868
This commit is contained in:
Phillip Webb 2014-05-15 15:47:44 +01:00
parent 08a6efff46
commit fa7da5925b
5 changed files with 43 additions and 19 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -214,7 +213,7 @@ public class CrshAutoConfiguration {
}
@PostConstruct
public void init() throws Exception {
public void init() {
FS commandFileSystem = createFileSystem(this.properties
.getCommandPathPatterns());
FS configurationFileSystem = createFileSystem(this.properties
@ -232,8 +231,7 @@ public class CrshAutoConfiguration {
start(context);
}
protected FS createFileSystem(String[] pathPatterns) throws IOException,
URISyntaxException {
protected FS createFileSystem(String[] pathPatterns) {
Assert.notNull(pathPatterns, "PathPatterns must not be null");
FS fileSystem = new FS();
for (String pathPattern : pathPatterns) {

View File

@ -87,10 +87,15 @@ public class BasicBatchConfigurer implements BatchConfigurer {
}
@PostConstruct
public void initialize() throws Exception {
this.transactionManager = createTransactionManager();
this.jobRepository = createJobRepository();
this.jobLauncher = createJobLauncher();
public void initialize() {
try {
this.transactionManager = createTransactionManager();
this.jobRepository = createJobRepository();
this.jobLauncher = createJobLauncher();
}
catch (Exception ex) {
throw new IllegalStateException("Unable to initialize Spring Batch", ex);
}
}
private JobLauncher createJobLauncher() throws Exception {

View File

@ -28,6 +28,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.stereotype.Component;
/**
@ -58,10 +59,9 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
}
@PostConstruct
protected void initialize() throws Exception {
protected void initialize() {
if (this.enabled) {
String platform = DatabaseType.fromMetaData(this.dataSource).toString()
.toLowerCase();
String platform = getDatabaseType();
if ("hsql".equals(platform)) {
platform = "hsqldb";
}
@ -78,4 +78,13 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
}
}
private String getDatabaseType() {
try {
return DatabaseType.fromMetaData(this.dataSource).toString().toLowerCase();
}
catch (MetaDataAccessException ex) {
throw new IllegalStateException("Unable to detect database type", ex);
}
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.jdbc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -86,7 +87,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
}
@PostConstruct
protected void initialize() throws Exception {
protected void initialize() {
boolean initialize = this.datasourceProperties.getProperty("initialize",
Boolean.class, true);
if (this.dataSource == null || !initialize) {
@ -101,11 +102,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
+ ".sql,classpath*:schema.sql,classpath*:data.sql";
}
List<Resource> resources = new ArrayList<Resource>();
for (String schemaLocation : StringUtils.commaDelimitedListToStringArray(schema)) {
resources.addAll(Arrays.asList(this.applicationContext
.getResources(schemaLocation)));
}
List<Resource> resources = getSchemaResources(schema);
boolean continueOnError = this.datasourceProperties.getProperty(
"continueOnError", Boolean.class, false);
@ -125,6 +122,21 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
}
}
private List<Resource> getSchemaResources(String schema) {
List<Resource> resources = new ArrayList<Resource>();
for (String schemaLocation : StringUtils.commaDelimitedListToStringArray(schema)) {
try {
resources.addAll(Arrays.asList(this.applicationContext
.getResources(schemaLocation)));
}
catch (IOException ex) {
throw new IllegalStateException("Unable to load resource from "
+ schemaLocation, ex);
}
}
return resources;
}
/**
* Determines if the {@code dataSource} being used by Spring was created from
* {@link EmbeddedDataSourceConfiguration}.

View File

@ -32,7 +32,7 @@ import com.mongodb.Mongo;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Mongo.
*
*
* @author Dave Syer
* @author Oliver Gierke
* @author Phillip Webb
@ -48,7 +48,7 @@ public class MongoAutoConfiguration {
private Mongo mongo;
@PreDestroy
public void close() throws UnknownHostException {
public void close() {
if (this.mongo != null) {
this.mongo.close();
}