Java Spring & H2 Dockerize -创建名为“org.springframework. Boot .autoconfigure.orm.jpa.HibernateJpaConfiguration”的bean时出错

pxq42qpu  于 2023-06-28  发布在  Spring
关注(0)|答案(2)|浏览(100)

我被分配了一个招聘过程的案例研究项目。我开发了一个Java Spring项目,并在内存数据库中使用了H2。
我必须将docker-compose.yml放到根文件夹中以将其dockerize。
即使我的应用程序在intellij上正确运行,它也不能在docker容器上运行。
他们将运行以下场景来运行我的应用程序。

This scenario will be run by our reviewers during the review process. Make sure that these steps work as exactly same without any additional steps !

 1. Clone project  
git clone {repository-git-url}

 2. Run docker compose file (docker-compose.yml) and expose your service on port 8080  
docker-compose up -d

 3. Send a health check request and receive HttpStatus 200   
curl -L -X GET 'http://localhost:8080/health'

为了将我的spring项目dockerize,我创建了一个Dockerfile文件。你可以在下面看到我的dockerfile:

FROM adoptopenjdk/openjdk8
EXPOSE 8080
ARG JAR_FILE=target/fleet-management-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} myapp.jar
ENTRYPOINT ["java","-jar","/myapp.jar"]

ENV SPRING_DATASOURCE_URL=jdbc:h2:mem:testdb
ENV SPRING_DATASOURCE_USERNAME=sa
ENV SPRING_DATASOURCE_PASSWORD=""

我还创建了一个docker-compose.yml文件。你可以看到下面:

version: '3'
services:
  myapp:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    environment:
      - SPRING_DATASOURCE_URL=jdbc:h2:mem:testdb
      - SPRING_DATASOURCE_USERNAME=sa
      - SPRING_DATASOURCE_PASSWORD=

Java 8和Sping Boot 2.7.1另外,你可以在下面看到我的application.properties文件:

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create

我可以使用命令“docker-compose up -d”启动容器,但无法使用命令“docker run -p8080:8080 {ID}”运行应用程序。它给了我关于H2数据库的错误。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependen
cyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.f
actory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed
; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException
: Cannot load driver class: org.h2.Driver

我如何解决这个问题?

kokeuurv

kokeuurv1#

这是程序无法找到相关jar时的常见场景。我要做的第一件事是查看这里的项目依赖文件,以及它关于h2驱动程序jar的导入配置的内容。
如果它被设置为provided或test,那么您可以相应地更改它们。
你可以使用上面的方法,也可以手动导入h2 jar并添加到classpath中。你需要做的步骤是将h2 jar复制到你的docker,你的run程序命令将是:
ENTRYPOINT [“java”,"-cp”,“myapp.jar:h2-1.4.200.jar”,“EntryClass”]
虽然这不是我刚才提到的推荐做法,但你可以使用shade插件。
更多阅读:What is the maven-shade-plugin used for, and why would you want to relocate Java packages?
希望这能帮上忙。

lx0bsm1f

lx0bsm1f2#

我已经解决了这个问题。我会分享我的文件。
application.properties:

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Dockerfile

FROM openjdk:8-jdk-alpine
RUN mkdir /app
COPY target/fleet-management-0.0.1-SNAPSHOT.jar /app/myapp.jar
RUN mkdir /h2-data
COPY h2-data/ /h2-data/
ENTRYPOINT ["java","-jar","/app/myapp.jar"]
CMD java -jar myapp.jar --spring.config.additional-location=file:/src/main/resources/application.properties

docker-compose.yml

version: "3"
services:
  myapp:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    depends_on:
      - database

  database:
    image: oscarfonts/h2:latest
    ports:
      - 1521:1521
      - 81:81
    volumes:
      - ./h2-data:/h2-data
      - ./scripts:/docker-entrypoint-initdb.d

以及pom.xml中的h2依赖关系:

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>compile</scope>
    </dependency>

相关问题