杭州市萧山区哪家做网站的公司好,如何使用wordpress主题,网站设计苏州,百度指数查询平台快速上手 Spring Boot#xff1a;基础使用详解 文章目录 快速上手 Spring Boot#xff1a;基础使用详解1、什么是SpringBoot2、Springboot快速入门搭建3、SpringBoot起步依赖4、SpringBoot自动配置#xff1a;以tomcat启动为例5、SpringBoot基础配置6、yaml7、多环境开发配置…快速上手 Spring Boot基础使用详解 文章目录 快速上手 Spring Boot基础使用详解1、什么是SpringBoot2、Springboot快速入门搭建3、SpringBoot起步依赖4、SpringBoot自动配置以tomcat启动为例5、SpringBoot基础配置6、yaml7、多环境开发配置 1、什么是SpringBoot
Spring Boot 是一个基于 Spring 的框架旨在简化 Spring 应用的配置和开发过程通过自动配置和约定大于配置的原则使开发者能够快速搭建独立、生产级别的应用程序。Spring程序缺点 配置繁琐依赖设置繁琐 SpringBoot程序优点 自动配置起步依赖简化依赖配置版本锁定内置tomcat服务器辅助功能内置服务器…… 2、Springboot快速入门搭建 新建项目选择Spring Initializr 因为官网网速会很慢这边Server URL可以修改成国内阿里云地址https://start.aliyun.com 在下一步之后可以选择Springboot版本并添加Spring web依赖最后点击完成就可以了 找到springboot启动类点击运行测试 在日志中我们可以看到java的版本tomcat版本和端口我们并没有配置tomcat这也说明了Springboot是内置了tomcat的 可以写个代码进行测试一下,之后访问localhost:8080/user
RestController
RequestMapping(/user)
public class UserController {GetMapping()public String getUser(){return hello springboot;}
}3、SpringBoot起步依赖
starter SpringBoot中常见项目名称定义了当前项目使用的所有项目坐标以达到减少依赖配置的目的
?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 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.0/version/parentgroupIdcom.demo/groupIdartifactIdspringboot-01-quickstart/artifactIdversion0.0.1-SNAPSHOT/versiondependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies
/projectproject xmlnshttp://maven.apache.org/POM/4.0.0xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancemodelVersion4.0.0/modelVersiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.5.0/versionpackagingpom/packagingpropertiesservlet-api.version4.0.1/servlet-api.version .../properties
/project
parent 所有SpringBoot项目要继承的项目定义了若干个坐标版本号依赖管理而非依赖以达到减少依赖冲突的目的
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancemodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.5.0/version/parentartifactIdspring-boot-starter-parent/artifactIdpackagingpom/packaging ...
/project实际开发 使用任意坐标时仅书写GAV中的G和AV由SpringBoot提供如发生坐标错误再指定version要小心版本冲突
?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 https://maven.apache.org/xsd/maven-4.0.0.xsdparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.0/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies
/projectSpringBoot程序启动
SpringBootApplication
public class SpringBootQuickStartApplication {public static void main(String[] args) {SpringApplication.run(SpringBootQuickStartApplication.class, args);}
}SpringBoot在创建项目时采用jar的打包方式 SpringBoot的引导类是项目的入口运行main方法就可以启动项目 使用maven依赖管理变更起步依赖项
4、SpringBoot自动配置以tomcat启动为例 查看自动配置的spring-boot-autoconfigure的包下的配置文件spring.factories 文件中包含所有Web容器(Tomcat)自动启动的配置类 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\ 找到Tomcat的启动类 进入ServerProperties类中查看代码可以看到端口号的set方法 public void setPort(Integer port) {this.port port;
}在ServerProperties类中存在一个静态内部类Tomcat配置了服务器的属性 查看默认配置spring-configuration-metadata.json文件大约在1213行 {name: server.port,type: java.lang.Integer,description: Server HTTP port.,sourceType: org.springframework.boot.autoconfigure.web.ServerProperties,defaultValue: 8080
}5、SpringBoot基础配置 配置格式 SpringBoot提供了多种属性配置方式
application.properties
server.port80application.yml
server:port: 81application.yaml
server:port: 82SpringBoot配置文件加载顺序 application.properties application.yml application.yaml
注意事项
SpringBoot核心配置文件名为applicationSpringBoot内置属性过多且所有属性集中在一起修改在使用时通过提示键关键字修改属性
6、yaml 什么是yaml和properties有什么区别 YAMLYAML Ain’t Markup Language一种数据序列化格式优点 容易阅读容易与脚本语言交互以数据为核心重数据轻格式 YAML文件扩展名 .yml主流.yaml yaml语法规则 大小写敏感 属性层级关系使用多行描述每行结尾使用冒号结束 使用缩进表示层级关系同层级左侧对齐只允许使用空格不允许使用Tab键 属性值前面添加空格属性名与属性值之间使用冒号空格作为分隔 student:name: xiaolinage: 18#表示注释 核心规则数据前面要加空格与冒号隔开 数据读取 stu:name: 小林age: 181.使用Value读取单个数据属性名引用方式${一级属性名.二级属性名……}
Value(${stu.name})
String name;Value(${stu.age})
Integer age;2.封装全部数据到Environment对象
注数组元素也只能一个个取出来 3.自定义对象封装指定数据【常用】
将对象添加Spring容器中在类上添加Component注解在类上添加ConfigurationProperties(prefix“指定前缀”)添加get和set方法toString方法在控制器中注入下面Enterprise对象
Component
ConfigurationProperties(prefix enterprise)
Data
public class Enterprise {private String name;private Integer age;private String tel;private String subject[];
}注如果使用lombok需要在pom.xml中导入坐标
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId
/dependency当输入ConfigurationProperties注解的时候自定义对象封装数据警告解决方案 在pom.xml文件添加以下
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional
/dependency7、多环境开发配置
在实际开发中项目的开发环境、测试环境、生产环境的配置信息是否会一致如何快速切换 多环境启动配置 yaml文件多环境启动 基本配置 新的写法 application.yml
spring:profiles:active: pro---
spring:config:activate:on-profile: pro
server:port: 80---
spring:config:activate:on-profile: test
server:port: 81---
spring:config:activate:on-profile: dev
server:port: 82properties文件多环境启动
#主启动配置文件 application.properties
spring.profiles.activepro#环境分类配置文件 application-pro.properties
server.port80#环境分类配置文件 application-dev.properties
server.port81#环境分类配置文件application-test.properties
server.port82多环境启动命令格式 带参数启动SpringBoot
# 指定哪个配置名
java –jar springboot.jar --spring.profiles.activetest
# 指定具体的参数
java –jar springboot.jar --server.port88
# 同时指定配置名 端口号
java –jar springboot.jar --server.port88 --spring.profiles.activetest多环境开发控制 Maven与SpringBoot多环境兼容步骤
先将application.properties中配置全部先注释了
Maven中设置多环境属性
profilesprofileiddev_env/idpropertiesprofile.activedev/profile.active/properties!-- 默认激活 --activationactiveByDefaulttrue/activeByDefault/activation/profileprofileidpro_env/idpropertiesprofile.activepro/profile.active/properties/profileprofileidtest_env/idpropertiesprofile.activetest/profile.active/properties/profile
/profilesSpringBoot中引用Maven属性 对资源文件开启对默认占位符的解析
buildpluginspluginartifactIdmaven-resources-plugin/artifactIdconfigurationencodingutf-8/encodinguseDefaultDelimiterstrue/useDefaultDelimiters/configuration/plugin/plugins
/build启动查看控制台输出的结果
注如果application-dev.properties中的配置也存在则优先使用这里面的配置再使用yml中的配置 文章转载自: http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.yrjxr.cn.gov.cn.yrjxr.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn