2017-02-15 6 views
2

Beim Konvertieren von Nicht-Spring-Anwendungen in Spring Boot möchten Sie die vorhandene context.xml-Datei im eingebetteten Tomcat verwenden.Context.xml im Spring Boot Embedded Tomcat lesen

mit Spring-Boot 1.5.1 und Tomcat 8.5.11

TomcatEmbeddedServletContainerFactory Konfiguration

@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); 
     } 
} 

Verfahren zur Überprüfung Datenbankverbindung,

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(); 
} 

Wie in bestehende context.xml laden Frühlingsstiefel.

Antwort

0

denke ich, als Variante Sie Pfad zu Ihrem tomcat Verzeichnis konfigurieren und verwenden Sie es im Frühjahr Boot spring doc with embedded container

+0

Ich versuche auch server.tomcat.basedir = target/tomcat in application.properties und hinzugefügt Kontext hinzuzufügen. XML im Verzeichnis conf, aber das hilft nicht. – Sheel

Verwandte Themen