4

While converting non spring application to Spring Boot want to use existing context.xml file in embedded tomcat.

Using Spring Boot 1.5.1 and Tomcat 8.5.11

TomcatEmbeddedServletContainerFactory Configuration

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {
            // Try-1 - Not Working
            if (context instanceof StandardContext) {
                StandardContext standardContext = (StandardContext) context;
                standardContext.setCopyXML(true);
            }

            // Try-2 - Not Working
            context.setConfigFile(Paths.get("/META-INF/context.xml").toFile().toURI().toURL());

            // Try-3 - Working - But due to very large and multiple configuration, java config will be cumbersome
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/myDB");
            resource.setType(DataSource.class.getName());
            resource.setAuth("Container");
            resource.setProperty("username", "user111");
            resource.setProperty("password", "password111");
            resource.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
            context.getNamingResources().addResource(resource);
       }
}

Method for checking database connection,

public void checkDb() {
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    DataSource datasource = (DataSource) envContext.lookup("jdbc/myDB");
    Connection con = datasource.getConnection();
}

So how to load existing context.xml in Spring Boot.

Sheel
  • 1,010
  • 1
  • 17
  • 30

1 Answers1

0

I think as a variant you can configure path to your tomcat dir and use it in spring boot spring doc with embedded container.

Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
Shamil
  • 95
  • 2
  • 7
  • I also try to add server.tomcat.basedir = target/tomcat in application.properties and added context.xml in conf directory but that doesn't help. – Sheel Feb 15 '17 at 11:29