Moh*_*rmi 3 java spring spring-boot
我正在使用 spring boot 构建一个胖罐子,该胖罐子使用
java -jar app.jar
Run Code Online (Sandbox Code Playgroud)
问题是我正在使用 docker 并且我想扩展 jar 内容以获得更好的可用性,我执行以下操作来提取:
unzip app.jar
Run Code Online (Sandbox Code Playgroud)
现在我用以下命令运行罐子:
java -cp "." org/springframework/boot/loader/JarLauncher
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxRsServer' defined in class path resource
Run Code Online (Sandbox Code Playgroud)
所以它无法找到我的bean,尽管它已配置:
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ImportResource("classpath:spring/beans_context.xml")
public class SpringBootJaxrsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJaxrsApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
beans_context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<import resource="classpath:META-INF/spring/*_context.xml"/>
Run Code Online (Sandbox Code Playgroud)
您使用的是哪个 Spring Boot 版本?从 2.3.0 开始,新的分层模式可用于打包 fat JAR。启用分层模式后,您就可以执行类似的操作来构建 Docker 映像。
FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM adoptopenjdk:11-jre-hotspot
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/resources/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Run Code Online (Sandbox Code Playgroud)
请注意,该命令是java org.springframework.boot.loader.JarLauncher.
Spring 博客的这篇文章对此示例进行了全面解释。有关 Spring Boot 2.3 中分层如何工作的更多信息,您可以参考这篇文章,同样来自 Spring 博客。
相反,如果您使用旧版本的 Spring Boot,则可以按照本文中的说明构建 Dockerfile 。在这种情况下,入口点将为java -cp app:app/lib/* hello.Application.
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10055 次 |
| 最近记录: |