在嵌入式Tomcat Spring Boot应用程序中配置JNDI URL资源

时间:2018-01-12 17:32:04

标签: spring-boot jndi embedded-tomcat-8

我正在使用嵌入式Tomcat服务器的Spring Boot应用程序。我的应用程序执行JNDI查找数据源和配置文件URL。 我能够配置JNDI数据源,但我无法在嵌入式Tomcat中配置JNDI URL。将WAR部署为独立Liberty服务器时,相同的应用程序正常工作,因为Liberty使用JNDI数据源和配置文件URL正确配置。

此外,JNDI数据源和URL在我的应用程序的jar中的spring配置文件中定义,这是常见的,我无法修改它,只能导入。

这就是我尝试配置嵌入式tomcat的方法

import java.net.URL;

import javax.sql.DataSource;

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Profile;

@SpringBootApplication(exclude = { JmxAutoConfiguration.class,DataSourceAutoConfiguration.class })
@ImportResource(locations = { "classpath:common-services.xml" })
public class MyApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    @Profile("dev")
    public TomcatEmbeddedServletContainerFactory tomcatFactory() {

        return new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

                tomcat.enableNaming();
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }

            @Override
            protected void postProcessContext(Context context) {
                ContextResource resource = new ContextResource();
                resource.setName("jdbc/myDataSource");
                resource.setType(DataSource.class.getName());
                resource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
                resource.setProperty("url", "jdbc:oracle:thin:@sadasd");
                resource.setProperty("username", "admin");
                resource.setProperty("password", "admin");
                resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
                context.getNamingResources().addResource(resource);

                ContextResource queryOnlyDataSourceResource = new ContextResource();
                queryOnlyDataSourceResource.setName("jdbc/queryOnlyDataSource");
                queryOnlyDataSourceResource.setType(DataSource.class.getName());
                queryOnlyDataSourceResource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
                queryOnlyDataSourceResource.setProperty("url", "jdbc:oracle:thin:@osajodoajo");
                queryOnlyDataSourceResource.setProperty("username", "admin");
                queryOnlyDataSourceResource.setProperty("password", "admin");
                queryOnlyDataSourceResource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
                context.getNamingResources().addResource(queryOnlyDataSourceResource);

                ContextResource dataSourceOrbisExport = new ContextResource();
                dataSourceOrbisExport.setName("jdbc/exportDataSource");
                dataSourceOrbisExport.setType(DataSource.class.getName());
                dataSourceOrbisExport.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
                dataSourceOrbisExport.setProperty("url", "jdbc:oracle:thin:@saddalsdkla");
                dataSourceOrbisExport.setProperty("username", "admin");
                dataSourceOrbisExport.setProperty("password", "admin");
                dataSourceOrbisExport.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
                context.getNamingResources().addResource(dataSourceOrbisExport);

                // File Configuration
                ContextResource engineConfigResource = new ContextResource();
                evolutionEngineConfigResource.setName("url/engineConfig");
                evolutionEngineConfigResource.setType(URL.class.getName()); resource.setProperty("protocol","file"); 
                evolutionEngineConfigResource.setProperty("file", "C:/configuration_DIT/EngineConfig.config");

                context.getNamingResources().addResource(engineConfigResource);

                ContextResource evolutionPresentationConfigResource = new ContextResource();
                presentationConfigResource.setName("url/presentationConfig");
                presentationConfigResource.setType(URL.class.getName()); resource.setProperty("protocol","file"); 
                evolutionPresentationConfigResource.setProperty("file", "C:/configuration_DIT/presentation.config");

                context.getNamingResources().addResource(presentationConfigResource);



            }
        };
    }

}

这就是错误所说的:

  

需要找不到找不到的'java.net.URL'类型的bean。

相关原因:org.springframework.beans.factory.BeanCreationException:创建名为'engineConfig'的bean时出错:init方法的调用失败;嵌套异常是javax.naming.NameNotFoundException:名称[url / engineConfig]未绑定在此Context中。无法找到[url]。

0 个答案:

没有答案