簡體   English   中英

如何在獨立(戰爭)和嵌入式 Tomcat 中為 Spring-Boot 應用程序配置數據源?

[英]How to configure DataSource for a Spring-Boot Application in a Standalone (war) and in an embedded Tomcat?

我有一個具有以下dependencyManagement的 Spring-Boot-Aplication:

<dependencyManagement>
  <dependencies>
    <dependency>
      <!-- Import dependency management from Spring Boot -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.5.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

以及以下dependencies

spring-boot-starter-jersey
spring-boot-starter-jdbc(exclusion:tomcat-jdbc) 
HikariCP(version:3.3.1)
ojdbc7

Tomcat 上,我將JNDI-Datasource配置為:

<Resource name="jdbc/myDS" 
  type="javax.sql.DataSource" 
  driverClassName="oracle.jdbc.driver.OracleDriver" 
  username="Superuser" 
  password="secret"
  url="jdbc:oracle:thin:@xxxDbX"      
  ../>

.properties -file 我添加了以下屬性:

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource    
spring.datasource.jndi-name=jdbc/myDS

由於Spring-Boot能夠從屬性配置DataSource ,我讓它這樣做並且我沒有為DataSource編寫額外的代碼。 部署在獨立的 Tomcat 中,它完美地工作。

從邏輯上講, Spring Boot嵌入式 Tomcat 中找不到JNDI-Resource並將應用程序作為Spring-Boot-Application 啟動,我得到了:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource.type' to java.lang.Class<javax.sql.DataSource>:

    Property: spring.datasource.type
    Value: org.apache.tomcat.jdbc.pool.DataSource
    Origin: class path resource [application.properties]:12:24
    Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<javax.sql.DataSource>]

Action:

Update your application's configuration

我希望能夠將應用程序作為Spring-Boot-Application 啟動,並構建一個可以部署在任何獨立 Tomcat 中war文件。

如果應用程序作為Spring-Boot-Application 啟動或者我必須擁有第二個.properties文件,這是否可以通過為第二個數據源添加properties來實現?

對我有用的解決方案是添加一個自定義屬性以用於嵌入式 Tomcat 服務器中數據源,如下所示:

# for a dedicated Tomcat
spring.datasource.jndi-name=jdbc/dirserver


# for the embedded Tomcat

embedded.datasource.driver-class-name=oracle.jdbc.OracleDriver
embedded.datasource.url=jdbc:oracle:thin:@//myServer:1521/xxxxx
embedded.datasource.username=superuser
embedded.datasource.password=topsecret

並在用@SpringBootApplication注釋的類中定義@Bean DataSource

@SpringBootApplication
public class MySbApplication extends SpringBootServletInitializer {

  private static final Logger lg = LoggerFactory.getLogger(MySbApplication.class);

  @Value("${embedded.datasource.username}")
  String username;
  @Value("${embedded.datasource.password}")
  String password;
  @Value("${embedded.datasource.driver-class-name}")
  String driverClassName;
  @Value("${embedded.datasource.url}")
  String url;

  @Bean(destroyMethod = "")
  public DataSource oracledataSoutŕce() throws SQLException {
    final OracleDataSource dataSource = new OracleDataSource();
    dataSource.setUser(username);
    dataSource.setPassword(password);
    dataSource.setURL(url);
    dataSource.setImplicitCachingEnabled(true);
    dataSource.setFastConnectionFailoverEnabled(true);
    return dataSource;
  }
}

我將在Github 中添加一個示例項目的鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM