使用postgres进行Spring Batch自动配置

时间:2016-03-03 13:34:18

标签: postgresql spring-boot spring-batch

我正在尝试使用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>

我明确表示&#39; hsqldb&#39;确保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而不是嵌入式数据库。?

在应用启动期间,我得到了一个说法:&#34;引起: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).

1 个答案:

答案 0 :(得分:0)

如果您使用的是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>

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