Как добавить файл context.xml на встроенный сервер tomcat

Я пытаюсь запустить приложение на встроенном сервере tomcat с помощью Spring -boot. У меня есть ресурсы и среды JNDI, настроенные в XML файле, помещенные под src/main/webapp/META-INF/context.xml в моем приложении. Когда я развертываю это приложение в стандартном контейнере сервлетов, он работает. Но когда я запускаю его с помощью встроенного сервера, он не распознает ресурсы JNDI, которые присутствуют в файлах контекста.

Я попытался добавить файл контекста в embeddedTomcatContainer программно, используя следующий код. Ссылка на этот блог -

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

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

            Context context = new StandardContext();
            File f = new File("D:\\Workspace\\SpringBoot\\src\\main\\webapp\\META-INF\\context.xml");


            try {
                context.setConfigFile(f.toURI().toURL());
                tomcat.getHost().addChild(context);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

Но я получаю следующее исключение. Может кто-нибудь, пожалуйста, дайте мне знать, как добавить файлы context.xml на встроенный сервер tomcat?

org.springframework.boot[email protected]100862e: startup date [Mon Nov 23 17:21:38 IST 2015]; root of context hierarchy
2015-11-23 17:21:39.811  INFO 69992 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-11-23 17:21:39.822  INFO 69992 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-11-23 17:21:39.822  INFO 69992 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2015-11-23 17:21:39.842 ERROR 69992 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase   : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to initialize component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[null]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.7.0_85]
    at java.util.concurrent.FutureTask.get(FutureTask.java:188) [na:1.7.0_85]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_85]
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[null]]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:106) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 6 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.apache.catalina.core.StandardContext.getObjectKeyPropertiesNameOnly(StandardContext.java:6247) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.getObjectNameKeyProperties(StandardContext.java:6228) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleMBeanBase.initInternal(LifecycleMBeanBase.java:61) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase.initInternal(ContainerBase.java:882) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.initInternal(StandardContext.java:6257) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 7 common frames omitted

2015-11-23 17:21:39.982  INFO 69992 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-11-23 17:21:39.982  INFO 69992 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1413 ms
2015-11-23 17:21:40.122  INFO 69992 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-11-23 17:21:40.122  INFO 69992 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-11-23 17:21:40.152 ERROR 69992 --- [           main] org.apache.catalina.core.ContainerBase   : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.7.0_85]
    at java.util.concurrent.FutureTask.get(FutureTask.java:188) ~[na:1.7.0_85]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:89) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:76) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:452) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication$1.getTomcatEmbeddedServletContainer(LnBootApplication.java:74) [classes/:na]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:167) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication.main(LnBootApplication.java:39) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) ~[na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.7.0_85]
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 6 common frames omitted

2015-11-23 17:21:40.152  WARN 69992 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2015-11-23 17:21:40.152  INFO 69992 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/D:/Workspaces/SpringBoot/ln-boot/target/classes/, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.0.RELEASE/spring-boot-starter-web-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter/1.3.0.RELEASE/spring-boot-starter-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot/1.3.0.RELEASE/spring-boot-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.0.RELEASE/spring-boot-autoconfigure-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.3.0.RELEASE/spring-boot-starter-logging-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar, file:/C:/Users/221008/.m2/repository/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/slf4j-api/1.7.13/slf4j-api-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.13/jcl-over-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/jul-to-slf4j/1.7.13/jul-to-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.13/log4j-over-slf4j-1.7.13.jar, file:/C:/Users/221008/.m2/repository/org/yaml/snakeyaml/1.16/snakeyaml-1.16.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-validation/1.3.0.RELEASE/spring-boot-starter-validation-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/hibernate/hibernate-validator/5.2.2.Final/hibernate-validator-5.2.2.Final.jar, file:/C:/Users/221008/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/C:/Users/221008/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/classmate/1.1.0/classmate-1.1.0.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.6.3/jackson-databind-2.6.3.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.3/jackson-annotations-2.6.3.jar, file:/C:/Users/221008/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.3/jackson-core-2.6.3.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-web/4.2.3.RELEASE/spring-web-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-aop/4.2.3.RELEASE/spring-aop-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-beans/4.2.3.RELEASE/spring-beans-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-context/4.2.3.RELEASE/spring-context-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-webmvc/4.2.3.RELEASE/spring-webmvc-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-expression/4.2.3.RELEASE/spring-expression-4.2.3.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/1.3.0.RELEASE/spring-boot-starter-tomcat-1.3.0.RELEASE.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.0.28/tomcat-embed-core-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.0.28/tomcat-embed-el-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-logging-juli/8.0.28/tomcat-embed-logging-juli-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.28/tomcat-embed-websocket-8.0.28.jar, file:/C:/Users/221008/.m2/repository/mysql/mysql-connector-java/5.1.37/mysql-connector-java-5.1.37.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/tomcat-dbcp/8.0.28/tomcat-dbcp-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/apache/tomcat/tomcat-juli/8.0.28/tomcat-juli-8.0.28.jar, file:/C:/Users/221008/.m2/repository/org/springframework/spring-core/4.2.3.RELEASE/spring-core-4.2.3.RELEASE.jar]
2015-11-23 17:21:40.152 ERROR 69992 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication.main(LnBootApplication.java:39) [classes/:na]
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:76) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:452) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.ln.risk.boot.LnBootApplication$1.getTomcatEmbeddedServletContainer(LnBootApplication.java:74) ~[classes/:na]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:167) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 8 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:337) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:89) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 14 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 16 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 18 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    ... 20 common frames omitted

EDIT:

Я также попробовал несколько дополнительных API-интерфейсов tomcat для загрузки файла context.xml(который имеет несколько ресурсов и среды JNDI) для экземпляра встроенного сервера, но я продолжаю получать исключение Null Pointer. PFB - различные API, которые я пытался использовать.

1.  tomcat.addContext("/my-boot", System.getProperty("user.dir")+ "/src/main/webapp/META-INF/context.xml");

2.  StandardContext context = new StandardContext();
context.setDefaultContextXml(System.getProperty("user.dir") + "/src/main/webapp/META-INF/context.xml");
tomcat.getHost().addChild(context);

Для обоих подходов я получаю исключение NullPointerException, а tomcat пытается получить имя моего ресурса. По имени методов API я считаю, что должен быть способ сделать это. Пожалуйста, помогите с этим.

2015-11-25 11:35:01.441  INFO 74428 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2015-11-25 11:35:01.471  WARN 74428 --- [ost-startStop-1] o.a.catalina.deploy.NamingResourcesImpl  : Failed to create MBean for naming resource [null]

java.lang.NullPointerException: null
    at javax.management.ObjectName.quote(ObjectName.java:1833) ~[na:1.7.0_85]
    at org.apache.catalina.mbeans.MBeanUtils.createObjectName(MBeanUtils.java:371) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.mbeans.MBeanUtils.createMBean(MBeanUtils.java:159) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.deploy.NamingResourcesImpl.initInternal(NamingResourcesImpl.java:939) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.StandardContext.initInternal(StandardContext.java:6261) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [tomcat-embed-core-8.0.28.jar:8.0.28]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_85]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_85]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_85]

Ответ 1

Не могли бы вы попробовать что-то вроде этого, импортировав встроенный tomcat context.xml в качестве ресурса:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:context.xml")
public class Application {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder()
                .showBanner(false)
                .sources(Application.class)
                .run(args);
}

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

EDIT: Однако для выполнения вышеприведенной реализации потребуется Spring XSD вручную анализировать XML-документ, поскольку мы пытаемся настроить чтение встроенного контейнера.

Итак, всегда лучше настраивать java-способ для настройки встроенных конфигураций context.xml, вот ссылка для загрузки doc Spring для чего-то подобного:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-embedded-container

Или, если вы просто хотите использовать jndi, вы можете ссылаться на нечто вроде this

Надеюсь, что это поможет.