2017-02-15 16 views
2

非スプリングアプリケーションをSpringブートに変換する際に、埋め込み型のTomcatで既存のcontext.xmlファイルを使用します。で、既存のcontext.xmlをロードするために、どのように春のブート1.5.1とTomcat 8.5.11Springブートでcontext.xmlを読む埋め込みTomcat

TomcatEmbeddedServletContainerFactory設定

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

データベース接続をチェックするための方法、

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

を使用して

春のブート。

答えて

0

私はあなたのTomcatのディレクトリへのパスを設定し、私もapplication.properties、コメントを追加しました文脈でserver.tomcat.basedir =ターゲット/ Tomcatを追加しようと春ブーツに spring doc with embedded container

+0

を使用することができバリアントとして考えます。 xmlをconfディレクトリに入れてもそれは役に立ちません。 – Sheel

関連する問題