使用 postgres 进行 Spring Batch 自动配置

Posted

技术标签:

【中文标题】使用 postgres 进行 Spring Batch 自动配置【英文标题】:Spring Batch Auto Configuration with postgres 【发布时间】:2016-06-16 20:47:08 【问题描述】:

我正在尝试使用 PosgreSQL 作为数据库的普通 Spring 批处理应用程序。

pom.xml:-

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>hsqldb</artifactId>
                    <groupId>org.hsqldb</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

我已明确排除“hsqldb”以确保 Spring 批处理使用 posgres。添加此依赖可以解决问题,但我不想在我的设置中使用 hsqldb。

配置类:-

@SpringBootApplication
@EnableBatchProcessing   
public class Application 

    private static Logger LOG = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) 

        SpringApplication.run(Application.class, args);
    

    

application.yml

spring:
  application:
    name: theapp
  batch:
    job:
      enabled: false
  profiles:
    active: local

---

spring:
  profiles: local
  data:
    mongodb:
      host: localhost
      database: dbname
  datasource:
    username: postgres
    password: password
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/org.postgresql.Driver

---

spring:
  profiles: abc
  data:
    mongodb:
      database: dbname
      host: abc.com
  datasource:
    username: postgres
    password: password
    url: jdbc:postgresql://abc.com:5432/dbname
    driver-class-name: org.postgresql.Driver

Junit 类:-

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes =  Application.class )
@ActiveProfiles(profiles =  "local" )
public class BillingEngineBatchTest 

    @Test
    public void test() 
        Assert.assertNotNull("");
    


问题

如何告诉 Spring Batch 使用 postgres 而不是嵌入式数据库。?

在应用启动期间,我收到一条异常消息:“Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE."

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "local" are currently active).
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 77 more
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "local" are currently active).

【问题讨论】:

通过从依赖项中删除 &lt;scope&gt;runtime&lt;/scope&gt;。因为这只会在构建可部署而不是在运行测试时包含依赖项。 @M.Deinum postgres:compile 也没有帮助。 为什么你有一个特定的个人资料以及你的application-local.yml 是什么样的(如果你有的话)。 @M.Deinum - 使用完整的 application.yml 更新问题。我将所有配置文件都保存在一个文件中。不同配置文件的 db (host,name) 可能不同。 你的测试类是错误的。不要使用@ContextConfiguration,而是使用@ SpringApplicationConfiguration 【参考方案1】:

如果您使用的是 postgres,那么您可能需要这个 pom.xml:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

我希望这可以解决您的问题

【讨论】:

以上是关于使用 postgres 进行 Spring Batch 自动配置的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中使用 Copy 命令从 Postgres 导出数据

Spring Data 配置和 Postgres 数据库集成(无 xml 配置)

带有用户名和密码的 Zonky + Spring Boot + Postgres + Flyway

在 Spring Boot 的同一个 pom.xml 中管理 H2 和 Postgres

Spring Boot jdbc模板在自定义模式下的postgres中找不到表

使用 Postgres docker 实例运行 Spring Boot docker 实例