SpringBoot:手动创建应用

news2024/10/7 6:43:51

Spring提供了在线的Spring Initialzr在线创建Spring Boot项目,为了更好的理解Spring Boot项目,这里我们选择手动创建。

1.新建Web应用

1.1 生成工程

首先要做是创建一个Java项目,这里我们选择使用Maven来支持,使用archetype:generate生成一个项目。

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DarchetypeVersion=1.4 
    -DgroupId=com.keyniu.dis 
    -DartifactId=DiveInSpring 
    -Dversion=0.1 
    -Dpackage=com.keyniu.dis 
    -DinteractiveMode=false

生成的项目结构很简单,整个目录结果如下图,根目录下存放pom.xml、src目录,src目录下又分为主目录(main)和测试目录(test)

1.2 添加Spring Boot依赖

为了让应用支持SpringBoot,只需要在pom.xml里添加Spring Boot依赖即可。这里我们目标是让项目支持Spring Boot提供Web接口。首先是设置项目的parent

<project>
    ...
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.0</version>
    </parent>
    ....
</project>

然后是添加spring-boot-starter-web依赖,支持Web接口开发

<project>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

为了支持当前项目的启动,需要定义SpringBootApplication注解,使用SpringApplication.run执行当前工程,我们创建一个引导类

package com.keyniu.dis;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DiveInMain {

    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return "hello," + name;
    }

    public static void main(String[] args) {
        SpringApplication.run(DiveInMain.class);
    }
}

通过DiveInMain.main方法启动后,我们通过curl查看/hello调用已经正常工作了。

randy@Randy:~$ curl -s http://192.168.31.52:8080/hello?name=randy
hello,randy

2. 不依赖<parent>

将pom.xml的parent设置为spring-boot-starter-parent确实很方便,然而有的时候,组织内部会有自己的parent pom定义,pom是单根继承,无法额外再加一个。我们需要拆解一下spring-boot-starter-parent的定义,它主要分为3部分:

  1. 依赖的版本管理,通过spring-boot-dependencies的dependencyManagement定义各个依赖的推荐版本
  2. 插件的使用管理,通过pluginManagement定义推荐的插件版本
  3. 自定义的profile,在spring-boot-starter-parent里定义了各种各样的profile实现特定场景定制的能力
2.1 依赖的版本管理

上面提到spring-boot-starter-parent提供了3个能力,分别是依赖的版本管理、插件的版本管理、自定义的Profile。想要在工程能不依赖spring-boot-starter-parent,通过引入spring-boot-dependencies,我们能做到继续使用Spring官方推荐的依赖版本,看如下的pom.xml定义,相比之前的版本,唯一的差异就是在dependencyManagement中使用了spring-boot-dependencies。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.keyniu.dis</groupId>
    <artifactId>DiveInSpring</artifactId>
    <version>0.1</version>

    <name>DiveInSpring</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.3.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
2.2 插件的使用管理

使用spring-boot-dependencies能实依赖的版本管理,通过IDEA执行DiveInMain看起来一切正常,直到我们脱离IDEA运行的时候,执行java -jar发现执行报错

PS D:\Workspace\DiveInSpring\target> java -jar .\DiveInSpring-0.1.jar
.\DiveInSpring-0.1.jar中没有主清单属性

我们知道java -jar执行的时候会查找MANIFEST.MF文件里定义的Main-Class配置,解压jar文件还发现MANIFEST.MF并没有设置Main-Class启动类,jar文件并不是FatJar,没有包含被依赖的组件

这个操作是由spring-boot-maven-plugin这个Maven插件完成的,不适用parent的话,我们要手动这个插件,配置start-class作为mainClass,startClass可能是JarLauncher、WarLauncher,它们定义在spring-boot-loader模块中。添加插件后,重新打包并使用java -jar执行,这个时候工程就能正常运行了。

<properties>
    <spring-boot.run.main-class>${start-class}</spring-boot.run.main-class>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>${spring-boot.run.main-class}</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

3. <parent>的完整能力

通过使用spring-boot-maven-plugin,我们的jar已经能够正常执行了。不过spring-boot-starter-parent提供的能力要比这个多得多。

3.1 资源文件处理

spring-boot-starter-parent默认会将src/main/resource下的所有文件当成资源文件,这些资源文件会被分成两类处理:

  1. application*,做变量引用(${变量名})替换,然后包含在资源文件中
  2. 其他,不做处理,直接复制进资源文件
<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
        </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
            <exclude>**/application*.yml</exclude>
            <exclude>**/application*.yaml</exclude>
            <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>
3.2 插件默认配置

此外spring-boot-starter-parent对插件还做了额外的配置,这里我们捡紧要的说

插件

配置

作用

maven-compiler-plugin

配置parameters=true,在编译的时候调用javac -parameters

在.class文件里保留参数名,供反射时读取

maven-jar-plugin

配置mainClass为start-class的值,JarLauncher

设置可执行jar的启动引导类

maven-war-plugin

配置mainClass为start-class的值,WarLauncher

设置可执行jar的启动引导类

git-commit-id-maven-plugin

配置git信息存储的文件,git.properties

获取打包时git仓库的版本号/分支等

spring-boot-maven-plugin

打包SpringBoot项目结构,包括第3方依赖

BOOT-INF/classes和BOOT-INF/lib

maven-shade-plugin

创建uber jar,资源的合并处理,创建可执行jar

META-INF下配置文件的合并处理,比如spring.handles

native-maven-plugin

使用GraalVM构建Spring成为native应用

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1795996.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Softing线上研讨会 | 如何使用dataFEED OPC Suite采集西门子SINUMERIK 840D SL CNC控制器数据

| (免费) 线上研讨会时间&#xff1a;2024年7月9日 16:00~16:30 / 22:00~22:30 无论是传统车间应用还是创新物联网解决方案&#xff0c;都依赖于机器和过程数据&#xff0c;因为这些数据对于提高生产效率、优化操作流程及实现智能化管理和决策而言都非常重要。因此&#xff0c…

Keil中for(int i=0;;)报错

一、报错 二、报错原因 定义变量i报错 这是C的写法&#xff0c;C语言不支持 用C语言格式应该为 int i0; for(int i;;;) {} c99支持第一种写法&#xff0c;如果使用gcc&#xff0c;可以指定c99模式。 三、指定c99模式

C#WPF数字大屏项目实战12--动态获取设备数据

1、如何获取设备实时数据 现在大屏上的数据都是静态的数据或后台构造的来源数据&#xff0c;在实际项目中现场数据应该来自现场的实时数据&#xff0c;这些数据有些是来自现场设备的动态数据&#xff0c;有些是来自其他系统推送的&#xff0c;有些需要主动查询其他业务&#xf…

Chroium 源码目录结构分析(2)

通过脚本&#xff0c;梳理统计chromium源码子目录的大小和功能情况&#xff1a; src根目录 import osdef get_total_directory_size(path, ignore_dirs):total_size 0for root, dirs, files in os.walk(path):dirs[:] [d for d in dirs if d not in ignore_dirs]for file i…

解析Pinterest公司的系统架构设计

最近我偶然发现了一个优秀的 YouTube 视频,“Pinterest 是如何在只有 6 名工程师的情况下扩展到 1100 万用户”&#xff08;https://www.youtube.com/watch?sicoeqLRKu5i1nnpbI&vQRlP6BI1PFA&featureyoutu.be&#xff09;以及以下参考文章,“Pinterest 的扩展之路 ——…

Qt 布局管理

布局基础 1)Qt 布局管理系统使用的类的继承关系如下图: QLayout 和 QLayoutItem 这两个类是抽象类,当设计自定义的布局管理器时才会使用到,通常使用的是由 Qt 实现的 QLayout 的几个子类。 2)Qt 使用布局管理器的步骤如下: 首先创建一个布局管理器类的对象。然后使用该…

linux基础-数据库建库建表

数据库建库建表 数据库内部&#xff1a;1、通过SQL解析器解析2、存储引擎 systemctl stop firewalld 关闭防火墙 1&#xff09;启动数据库mysql #启动systemctl start mariadb #检查进程ps -ef|grep mysql|grep -v mysql #检查端口netstat -lnt #登录测试&#xff08;后…

node.js漏洞——

一.什么是node.js 简单的说 Node.js 就是运行在服务端的 JavaScript。 Node.js 是一个基于 Chrome JavaScript 运行时建立的一个平台。 Node.js 是一个事件驱动 I/O 服务端 JavaScript 环境&#xff0c;基于 Google 的 V8 引擎&#xff0c;V8 引擎执行 Javascript 的速度非常…

49.线程池的关闭方法

shutdown方法 1.线程池状态变为shutdown 2.不会接收新任务 3.已提交的任务会执行完 4.此方法不会阻塞调用线程执行 ExecutorService executorService = Executors.newFixedThreadPool(2);executorService.submit(() -> {log.debug("task1 running");try {TimeUnit…

云原生架构案例分析_3.某快递公司核心业务系统云原生改造

名称解释&#xff1a; 阿里云ACK&#xff1a;阿里云容器服务 Kubernetes 版 ACK&#xff08;Container Service for Kubernetes&#xff09;集成Kubernetes网络、阿里云VPC、阿里云SLB&#xff0c;提供稳定高性能的容器网络。本文介绍ACK集群网络及阿里云网络底层基础设施的重要…

Spring Boot中整合Jasypt 使用自定义注解+AOP实现敏感字段的加解密

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

Bidirectional Copy-Paste for Semi-Supervised Medical Image Segmentation

文章目录 1. 问题背景2. 本文方法2.1. 模型图2.2. 损失函数 2. 模型的训练流程图3. 实验 1. 问题背景 &#xff08;1&#xff09;在半监督医学图像分割任务中&#xff0c;标签数据和无标签数据之间存在经验失配问题。 &#xff08;2&#xff09;如果采用分隔的方式或者采用不一…

【区块链】truffle测试

配置区块链网络 启动Ganache软件 使用VScode打开项目的wordspace 配置对外访问的RPC接口为7545&#xff0c;配置项目的truffle-config.js实现与新建Workspace的连接。 创建项目 创建一个新的目录 mkdir MetaCoin cd MetaCoin下载metacoin盒子 truffle unbox metacoincontra…

面试(五)

目录 1. 知道大顶端小顶端吗&#xff0c;代码怎么区分大顶端小顶端 2. 计算机中栈地址与内存地址增长方向相反吗&#xff1f; 3. %p和%d输出指针地址 4. 为什么定义第二个变量时候&#xff0c;地址反而减了 5. 12&#xff0c;32&#xff0c;64位中数据的占字节&#xff1f…

告别维修响应慢、费用纠纷,物业报修系统为客服人员减压增效

大家都知道物业报修系统是物业服务企业的得力助手&#xff0c;不仅帮助物业服务企业提升了业主满意度&#xff0c;还增强了物业和业主之间的粘度。但是&#xff0c;我们却忽略了物业报修系统对于物业客服人员带来了哪些工作上的便捷&#xff1f; 作为物业客服人员的你&#xff…

14 个必须了解的微服务设计原则

想象一下&#xff0c;一个机场有各种各样的业务&#xff0c;每个部门都是一个精心设计的微服务&#xff0c;专门用于预订、值机和行李处理等特定操作。机场架构必须遵循这个精心设计的架构的基本设计原则&#xff0c;反映微服务的原则。 例如&#xff0c;航空公司独立运营&…

搜索与图论:宽度优先搜索

搜索与图论&#xff1a;宽度优先搜索 题目描述参考代码 题目描述 输入样例 5 5 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0输出样例 8参考代码 #include <iostream> #include <algorithm> #include <cstring> using namespace std;const int N …

chatglm-6b部署加微调

这里不建议大家使用自己的电脑&#xff0c;这边推荐使用UCloud优刻得-首家公有云科创板上市公司 我们进去以后会有一个新人优惠&#xff0c;然后有一个7天30元的购买&#xff0c;购买之后可以去选择镜像&#xff0c;然后在镜像处理的这个位置&#xff0c;可以选择镜像&#xf…

浙江零排参加全国水科技大会暨技术装备成果展览会(成都)并作主论坛演讲

2024年5月13日-15日中华环保联合会、福州大学、上海大学等联合举办的2024年全国水科技大会暨技术装备成果展览会在成都顺利举办。浙江零排城乡规划发展有限公司司受邀参加&#xff0c;首日有幸听取徐祖信院士、任洪强院士、汪华林院士等嘉宾的主旨报告。主旨报告后&#xff0c;…

麒麟操作系统rpm ivh安装rpm包卡死问题分析

夜间变更开发反应&#xff0c;rpm -ivh 安装包命令夯死&#xff0c;无执行结果&#xff0c;也无报错 排查 &#xff1a; 1、top 查看无进程占用较高进程存在&#xff0c;整体运行平稳 2、df -h 查看磁盘并未占满 3、其他服务器复现该命令正常执行 4、ps -ef|grep rpm 查看安装…