Spring初识

本节包括:

  • spring5的新特性
  • springboot和springcloud的优势
  • 基本打jar包以及运行
  • 不使用parent引入spring依赖

spring 5.x支持Java8+、kotlin语言;支持webflux异步编程模式;去掉了一些支持,例如velocity模板引擎。

springboot和springcloud,开箱即用;非功能性封装,深度整合生态圈,注重运维,将最佳实践固化到框架中;

初始化项目、打包、执行jar

使用spring initializer快速搭建应用,引入web和actuator两个依赖,编写hello spring Controller后执行。

> curl http://localhost:8080/hello
hello spring
// 探测应用状态
> curl http://localhost:8080/actuator/health
{"status":"UP"} 

maven快速打包,-Dmaven.test.skip=true跳过单元测试

> mvn clean package -Dmaven.test.skip

执行jar,此时应用作为一个独立的进程执行,可以接受HTTP请求。

> java -jar geektime-hello-spring-0.0.1-SNAPSHOT.jar

Spring Boot默认打可执行jar包,jar包中包含所有依赖jar包,并且包含web容器,因此可以直接运行。也可以打成普通war包,使用外部容器(例如Tomcat)运行。实际代码打出的jar包是.original包,不可执行。

不使用springboot parent如何配置pom文件?

当某些情况,例如我们需要依赖另外一个包作为parent,此时我们没办法继承springboot的pom了。

<!--管理依赖,引入spring-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.5.6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • dependencyManagement用来引入SpringBoot的依赖管理,管理依赖、version、exclusion等,repackage是为了能够打出可执行的jar包

  • 使用repackage,作用是告诉maven什么时候执行plugin动作。因为我们如果不用spring-boot-starter,那就用不了Spring Boot的一些默认配置,在Maven的配置里默认是不会调用Spring Boot的Fat Jar的打包逻辑,所以需要配置一下。

posted @   Awecoder  阅读(56)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示