springboot读取tomcat-context.xml [英] springboot read tomcat-context.xml

查看:850
本文介绍了springboot读取tomcat-context.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我们在tomcat应用程序服务器上运行了一个spring应用程序.我想在JUnit中添加与Springboot的集成测试.我现在遇到的问题是嵌入式tomcat服务器未存储我们的数据源,因此无法查找...如果服务器配置中未定义数据源,则我们的资源中应该有一个后备的"context.xml"用过的.但是嵌入式tomcat没有读取此配置,我只是不知道如何执行此操作. 以下是我的JUnit测试中的失败点,因为它无法在嵌入式tomcat中找到此JNDI-Name:

Hi we got a spring application running on tomcat application-server. I want to add integration tests with Springboot in JUnit. I now got the problem that the embedded tomcat server does not store our datasource and therefore it cannot be looked up... If the server configuration does not have a datasource defined we got a fallback "context.xml" in our resources that should be used. But the embedded tomcat is not reading this configuration and I just cannot figure out, how to do this. The following is the point of failure in my JUnit Test, since it cannot find this JNDI-Name in the embedded tomcat:

@Bean
public DataSource dataSource()
{
  return new JndiDataSourceLookup().getDataSource("jdbc/myDB");
}

fallback context.xml看起来像这样:

the fallback context.xml looks like this:

<Context>
<!-- HSQL -->
   <Resource name="jdbc/myDB" auth="Container"
             type="javax.sql.DataSource" driverClassName="org.hsqldb.jdbcDriver"
             url="jdbc:hsqldb:mem:mymemdb;shutdown=true" username="SA" password=""
             maxTotal="100" maxIdle="5" maxWaitMillis="10000"/>

   <Manager pathname=""/>
</Context>

我如何在启动时将此文件推送到嵌入式tomcat中?

how can I push this file into the embedded tomcat on startup?

推荐答案

Spring Boot提供了许多方法来

Spring Boot offer many ways to externalize your configuration...

您可以定义数据源属性在您的tomcat/conf/Catalina/<host>上下文描述符中.

You can define your datasource properties in your tomcat/conf/Catalina/<host> context descriptors.

您可以定义您的jndi名称:

You could define your jndi name:

<Context>
    <Parameter name="spring.datasource.jndi-name" value="/pathToYourDatasource" />
</Context>

或定义数据源:

<Context>
    <Parameter name="spring.datasource.url" value="your url" />
    <Parameter name="spring.datasource.username" value="your username" />
    <Parameter name="spring.datasource.password" value="your password" />
</Context>

甚至定义application.properties的路径并在此处设置您的配置:

Even define the path to an application.properties and set here your configuration:

<Context>
    <Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>

这种方式不必通过编程方式设置硬编码的数据源,并且可以在src/test/resources/application.properties中放置另一个数据库配置以进行测试:

This way is not necessary set programmatically your hardcoded datasource and you can put another database configuration for testing in src/test/resources/application.properties:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

这篇关于springboot读取tomcat-context.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆