可以在自己的电脑上做网站吗,做淘客网站用什么程序,网站 管理,做网站需要机吗Spring提供了在线的Spring Initialzr在线创建Spring Boot项目#xff0c;为了更好的理解Spring Boot项目#xff0c;这里我们选择手动创建。
1.新建Web应用
1.1 生成工程
首先要做是创建一个Java项目#xff0c;这里我们选择使用Maven来支持#xff0c;使用archetype:ge…Spring提供了在线的Spring Initialzr在线创建Spring Boot项目为了更好的理解Spring Boot项目这里我们选择手动创建。
1.新建Web应用
1.1 生成工程
首先要做是创建一个Java项目这里我们选择使用Maven来支持使用archetype:generate生成一个项目。
mvn archetype:generate -DarchetypeGroupIdorg.apache.maven.archetypes -DarchetypeArtifactIdmaven-archetype-quickstart -DarchetypeVersion1.4 -DgroupIdcom.keyniu.dis -DartifactIdDiveInSpring -Dversion0.1 -Dpackagecom.keyniu.dis -DinteractiveModefalse
生成的项目结构很简单整个目录结果如下图根目录下存放pom.xml、src目录src目录下又分为主目录(main)和测试目录(test) 1.2 添加Spring Boot依赖
为了让应用支持SpringBoot只需要在pom.xml里添加Spring Boot依赖即可。这里我们目标是让项目支持Spring Boot提供Web接口。首先是设置项目的parent
project...parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.3.0/version/parent....
/project
然后是添加spring-boot-starter-web依赖支持Web接口开发
projectdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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调用已经正常工作了。
randyRandy:~$ curl -s http://192.168.31.52:8080/hello?namerandy
hello,randy
2. 不依赖parent
将pom.xml的parent设置为spring-boot-starter-parent确实很方便然而有的时候组织内部会有自己的parent pom定义pom是单根继承无法额外再加一个。我们需要拆解一下spring-boot-starter-parent的定义它主要分为3部分
依赖的版本管理通过spring-boot-dependencies的dependencyManagement定义各个依赖的推荐版本插件的使用管理通过pluginManagement定义推荐的插件版本自定义的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 version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.keyniu.dis/groupIdartifactIdDiveInSpring/artifactIdversion0.1/versionnameDiveInSpring/namepropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.target/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion3.3.0/version/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion3.3.0/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependency/dependencies/project2.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作为mainClassstartClass可能是JarLauncher、WarLauncher它们定义在spring-boot-loader模块中。添加插件后重新打包并使用java -jar执行这个时候工程就能正常运行了。
propertiesspring-boot.run.main-class${start-class}/spring-boot.run.main-class
/propertiesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdexecutionsexecutionidrepackage/idgoalsgoalrepackage/goal/goals/execution/executionsconfigurationmainClass${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下的所有文件当成资源文件这些资源文件会被分成两类处理:
application*做变量引用(${变量名})替换然后包含在资源文件中其他不做处理直接复制进资源文件
buildresourcesresourcedirectory${basedir}/src/main/resources/directoryfilteringtrue/filteringincludesinclude**/application*.yml/includeinclude**/application*.yaml/includeinclude**/application*.properties/include/includes/resourceresourcedirectory${basedir}/src/main/resources/directoryexcludesexclude**/application*.yml/excludeexclude**/application*.yaml/excludeexclude**/application*.properties/exclude/excludes/resource/resources
/build
3.2 插件默认配置
此外spring-boot-starter-parent对插件还做了额外的配置这里我们捡紧要的说 插件 配置 作用 maven-compiler-plugin 配置parameterstrue在编译的时候调用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应用