1.spring boot是配置好的spring集成框架,约定大于配置
2.微服务:把service拆出来独立运行在各个机器上。看下面这两篇论文
- 原文地址:http://martinfowler.com/articles/microservices.html
- 翻译:https://www.cnblogs.com/liuning8023/p/4493156.html
3.从官网下模板创建spring boot项目
从https://start.spring.io/里找generate项目,下载zip包,用Idea以maven形式导入
4.通过Idea的Spring Initializer创建
以上二种方式,要自己选依赖的组件
pom开头parent标签指定父依赖是spring-boot-starter-parent,负责版本管理与打包
web主要依赖包是spring-boot-starter-web
热部署是spring-boot-starter-devtools
4.项目架构
1.java下的程序主入口:
2.resource下的application.properties
3.resource下的static目录
4.resource下的templates目录
5.springboot的主类用SpringBootApplication注解装配,他会去扫描同级目录下的包注解
6.配置文件修改
server.port=8081 #改端口号,每一个springboot服务一个端口
7.导出的jar包用java jar包路径运行即可
8.resource目录下可以放banner.txt放启动字符
9.自动配置原理:
看pom文件,点入父标签,进入可以看到spring-boot-dependencies依赖
SpringApplication run方法启动程序
注解:@SpringBootApplication
- @SprintBootConfiguration
- @Configuration
- @Component
- @EnableConfiguration
- AutoConfigurationPackage
总结下来,就是会找sprint-boot-autoconfigure包里的/META-INF/spring.factories文件,当引入某个starter时相应的配置生效
10.主类:
SpringApplication类注解初始化项目,SpringApplication的run方法拿这些配置来跑
11.yaml配置文件
格式:
key: value
server:
port: 8081
path: /hello
yaml比properties先进的地方在于语法支持的多,甚至可以存储对象
双引号不转义,单引号转义
存对象
properties:
student.name="a"
yaml:
studnet:
name: "a"
行内写法:
student: {name: "a", age: 12}
数组:
行内写法:
studnet: 【{},{}】
多行写法:
pets:
- cat
- dog
yaml 对空格的要求十分高,一定要对齐
12.yaml配置注入:
通过ConfigurationProperties注解指定prefix来引入配置
@ConfigurationProperties(prefix = "person")
13.Property文件注入
@PropertySource(value = "classpath:person.properties")
@Component //注册bean public class Person { //直接使用@value @Value("${person.name}") //从配置文件中取值 private String name; @Value("#{11*2}") //#{SPEL} Spring表达式 private Integer age; @Value("true") // 字面量 private Boolean happy; 。。。。。。 }
14.JSR303校验
@Validated注解表明要校验
可以接着使用其中定义的其他格式规范注解,比如@Email
15.多config文件
可以在jar里用命令行参数spring.config.location指定config文件位置。默认是项目根目录下config,项目根目录, 资源根目录下config,资源根目录
用spring.profiles.acitve=test指定配置文件后缀
yml多文档模块:
---分割模块
spring:
profiles: dev
指定模块名
y