Java 在Spring Boot嵌入式Tomcat中读取context.xml

Java 在Spring Boot嵌入式Tomcat中读取context.xml,java,spring-boot,tomcat8,embedded-tomcat-8,Java,Spring Boot,Tomcat8,Embedded Tomcat 8,在将非spring应用程序转换为spring引导时,您希望使用嵌入式tomcat中现有的context.xml文件 使用Spring Boot 1.5.1和Tomcat 8.5.11 TomcatEmbeddedServletContainerFactory配置 @Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFact

在将非spring应用程序转换为spring引导时,您希望使用嵌入式tomcat中现有的context.xml文件

使用Spring Boot 1.5.1和Tomcat 8.5.11

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

因此,如何在Spring Boot中加载现有的context.xml。

我认为作为一种变体,您可以配置tomcat目录的路径,并在Spring Boot中使用它。

我还尝试在application.properties中添加server.tomcat.basedir=target/tomcat,并在conf目录中添加context.xml,但这没有帮助。