Skip to content

Instantly share code, notes, and snippets.

@ldziedziul
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save ldziedziul/d492a1e417e58d703b40 to your computer and use it in GitHub Desktop.

Select an option

Save ldziedziul/d492a1e417e58d703b40 to your computer and use it in GitHub Desktop.
Multiple wars of Spring Boot app on the single Tomcat instance
/*
* AppWebXml.java
*
*/
package pl.dziedziul;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import pl.dziedziul.app.web.Application;
public class AppWebXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
final SpringApplicationBuilder applicationBuilder = application.sources(Application.class);
String applicationName = extractContextName(application);
//app will load properties from /home/tomcat/conf/<context-path>.properties
applicationBuilder.properties("spring.config.name:"+applicationName);
return applicationBuilder;
}
private String extractContextName(SpringApplicationBuilder application) {
String contextName = ((WebappClassLoader) application.application().getClassLoader()).getContextName();
if (!StringUtils.isEmpty(contextName)){
contextName = contextName.substring(1);
} else {
throw new RuntimeException("Cannot get context name of webapp");
}
return contextName;
}
}
#mind the slash at the end.
export CATALINA_OPTS="-Dspring.config.location=file:/home/tomcat/conf/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment