ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Spring Boot项目创建

Spring Boot项目创建 一、项目创建Spring Boot是在Spring框架上创建的一个全新的框架其设计目的是简化Spring应用的搭建和开发过程。开启Spring Boot有许多种方法可供选择这里仅介绍使用idea来构建一个简单的Spring Boot项目。1 首先创建一个空项目然后在项目上面鼠标右键选择New-Module如下图所示2 接着选择Spring Initializr点击next3 输入包名等信息后点击next4 输入模块名字和选择对应的路径然后点击finish至此我们一个最基础的springboot项目就创建好了目录结构如下二、项目演示2.1 启动类项目根目录下生成了一个artifactIdApplication命名规则的入口类为了演示简单不再新建控制器直接在入口类中编写代码packagecom.wno704.boot;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerSpringBootApplicationpublicclassBootApplication{publicstaticvoidmain(String[]args){SpringApplication.run(BootApplication.class,args);}RequestMapping(/)Stringindex(){returnhello spring boot;}}2.2 使用maven构建项目2.3 运行项目点击run或者debug都可以这里我们点击了debug项目启动成功如下所示2.4 测试项目浏览器访问http://localhost:8080会显示如下三、打包发布3.1 打包成可执行的 jar 包默认情况下通过 maven 执行 package 命令后会生成 jar 包且该 jar 包会内置了 tomcat 容器因此我们可以通过 java -jar 就可以运行项目如下图当用maven构建项目成功后会在项目target目录下生成发布包如下图3.2 打包成部署的 war 包1让 SpringbootApplication 类继承 SpringBootServletInitializer 并重写 configure 方法如下SpringBootApplicationpublicclassSpringbootApplicationextendsSpringBootServletInitializer{OverrideprotectedSpringApplicationBuilderconfigure(SpringApplicationBuilderapplication){returnapplication.sources(SpringbootApplication.class);}publicstaticvoidmain(String[]args){SpringApplication.run(SpringbootApplication.class,args);}}2修改 pom.xml 文件将 jar 改成 war如下packagingwar/packaging在标签内配置项目名...build...finalNamewno704/finalName/build...3移除内置 tomcatdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId!-- 打 war 包时排除内置 tomcat需要放开下边的注释 --exclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactId/exclusion/exclusions/dependency!--war打包的时候可以不用包进去别的设施会提供。事实上该依赖理论上可以参与编译测试运行等周期。相当于compile但是打包阶段做了exclude操作--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactIdscopeprovided/scope/dependency打包成功后将 war 包部署到 tomcat 容器中运行即可。四、pom.xml解释4.1 pom.xml打开pom.xml可看到配置如下?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.3.RELEASE/versionrelativePath/!-- lookup parent from repository --/parentgroupIdcom.wno704/groupIdartifactIdboot/artifactIdversion0.0.1-SNAPSHOT/versionnameboot/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project4.2 spring-boot-starter-parentspring-boot-starter-parent指定了当前项目为一个Spring Boot项目它提供了诸多的默认Maven依赖具体可查看目录F:\repo\org\springframework\boot\spring-boot-dependencies\2.3.3.RELEASE下的spring-boot-dependencies-2.3.3.RELEASE.pom文件properties......solr.version8.5.2/solr.versionspring-amqp.version2.2.10.RELEASE/spring-amqp.versionspring-batch.version4.2.4.RELEASE/spring-batch.versionspring-data-releasetrain.versionNeumann-SR3/spring-data-releasetrain.versionspring-framework.version5.2.8.RELEASE/spring-framework.versionspring-hateoas.version1.1.1.RELEASE/spring-hateoas.versionspring-integration.version5.3.2.RELEASE/spring-integration.versionspring-kafka.version2.5.5.RELEASE/spring-kafka.versionspring-ldap.version2.3.3.RELEASE/spring-ldap.versionspring-restdocs.version2.0.4.RELEASE/spring-restdocs.versionspring-retry.version1.2.5.RELEASE/spring-retry.versionspring-security.version5.3.4.RELEASE/spring-security.versionspring-session-bom.versionDragonfruit-RELEASE/spring-session-bom.versionspring-ws.version3.0.9.RELEASE/spring-ws.versionsqlite-jdbc.version3.31.1/sqlite-jdbc.versionsun-mail.version1.6.5/sun-mail.versionthymeleaf.version3.0.11.RELEASE/thymeleaf.versionthymeleaf-extras-data-attribute.version2.0.1/thymeleaf-extras-data-attribute.versionthymeleaf-extras-java8time.version3.0.4.RELEASE/thymeleaf-extras-java8time.versionthymeleaf-extras-springsecurity.version3.0.4.RELEASE/thymeleaf-extras-springsecurity.versionthymeleaf-layout-dialect.version2.4.1/thymeleaf-layout-dialect.versiontomcat.version9.0.37/tomcat.versionunboundid-ldapsdk.version4.0.14/unboundid-ldapsdk.versionundertow.version2.1.3.Final/undertow.versionversions-maven-plugin.version2.7/versions-maven-plugin.versionwebjars-hal-browser.version3325375/webjars-hal-browser.versionwebjars-locator-core.version0.45/webjars-locator-core.versionwsdl4j.version1.6.3/wsdl4j.versionxml-maven-plugin.version1.0.2/xml-maven-plugin.versionxmlunit2.version2.7.0/xmlunit2.version/properties需要说明的是并非所有在标签中配置了版本号的依赖都有被启用其启用与否取决于您是否配置了相应的starter。比如tomcat这个依赖就是spring-boot-starter-web的传递性依赖下面将会描述到。当然我们可以手动改变这些依赖的版本。比如我想把thymeleaf的版本改为3.0.0.RELEASE我们可以在pom.xml中进行如下配置propertiesthymeleaf.version3.0.0.RELEASE/thymeleaf.version/properties4.3 spring-boot-starter-webSpring Boot提供了许多开箱即用的依赖模块这些模块都是以spring-boot-starter-XX命名的。比如要开启Spring Boot的web功能只需要在pom.xml中配置spring-boot-starter-web即可dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency因为其依赖于spring-boot-starter-parent所以这里可以不用配置version。保存后Maven会自动帮我们下载spring-boot-starter-web模块所包含的jar文件。我们也可以手动排除一些我们不需要的依赖。比如spring-boot-starter-web默认集成了tomcat假如我们想把它换为jetty可以在pom.xml中spring-boot-starter-web下排除tomcat依赖然后手动引入jetty依赖dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdexclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactId/exclusion/exclusions/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jetty/artifactId/dependency/dependencies4.4 spring-boot-maven-pluginspring-boot-maven-plugin为Spring Boot Maven插件提供了把项目打包成一个可执行的超级JARuber-JAR,包括把应用程序的所有依赖打入JAR文件内并为JAR添加一个描述文件其中的内容能让你用java -jar来运行应用程序。搜索public static void main()方法来标记为可运行类。代码下载
返回列表