spring boot 学习笔记

来源:互联网 发布:用仿真软件发cscd 编辑:程序博客网 时间:2024/06/01 08:54
springboot 的配置文件名为application.properties
默认的位置在classpath根目录,或者classpath:/config
默认的文件名可以用 --spring.config.name=xxxx 后缀可以省略
默认的文件路径可以用 --spring.config.location=xxxx 配置文件需要指定全路径 包含目录加文件名。
加载外部的配置可以用 @propertySource("classpath:xxxx") 来指定  可使用多个。


@EnableAutoConfigration 作用:从classpath中搜索所有 META-INF/spring.factories 配置文件
然后将其中 org.springframework.boot.autoconfigure.EnableAutoConfiguration key 对应的配置项加载到spring 容器


ImportSelecor该接口的方法返回都会纳入到spring 容器中
SpringFactoriesLoader 这个类可以从classpath中加载类到spring 容器




ApplicationContextInitializer 接口是在spring容器执行refreshed之前的一个回调
使用步骤
1.写一个类,实现ApplicationContextInitializer接口
2.注册ApplicationContextInitializer
注册方法
1.springApplication.addInitializers
2.配置文件里面 配置 context.initialzer.class 指定 可配置多个用逗号隔开
3.可以通过 spring.factories机制


CommandLineRunner ApplicatioinRunner 接口是在容器启动成功后的最后一步回调
使用步骤
1.写一个类,实现CommandLineRunner接口
2.把该类加入到spring容器中
可以通过@order() 注解 或者ordered接口来控制执行顺序




CommandLineRunner ApplicatioinRunner区别
1.在于参数不一样  CommandLineRunner 原始参数
ApplicatioinRunner 的ApplicatioinArguments对原始参数进一步封装了。


springBootApplication 默认扫描的包是当前包及子包,可以通过 Scanbasepackage 指定






springBoot的执行过程。
1.判断是否是web环境。
2.加载所有classpath下面 META-INF/spring.factories ApplicationContextInitalzer
3.加载所有classpath下面 META-INF/spring.factories ApplicationListener
4.推断main方法所在的类。
5.开始执行run方法,
6.设置java.awt.headless系统变量
7.加载所有classpath下面 META-INF/spring.factories SpringApplicationRunListener
8.执行甩有的SpringApplicationRunListener 的start方法
9.实例化ApplicationArguments对象
10.创建environment
11.配置 environment, 主要是把run方法的参数配置到environment
12.执行所有SpringApplicationRunListener的environmentPrepared方法
13.如果不是web环境,但是是web的environment,则把web的environment转换成标准的environment
14.打印Banner.
15.初始化applicationcontext,如果是web环境,则实例化AnnotationConfigEmbeddeWebApplicationContext
否则实例化AnnotationConfigApplicationContext
16.如果beanNameGenerator不为空,就把beanNameGenerator注入到context中去
17.回调所有的ApplicationContextInitalizer方法
18.执行所有SpringApplicationRunlistener的contextprepared方法。
19.依次往spring容器中注入,ApplicationArguments,Banner
20.加载所有的源到context中去
21.执行所有SpringApplicationRunlistener的contextLoaded方法
22.执行context的refresh方法,并且调用context的registerShutdownHook方法
23.回调,获取容器中所有的ApplicationRunner、CommandLineRunner接口,然后排序,依次调用
24.执行所有SpringApplicationRunlistener的finished方法




springboot里面使用jsp ,需要加入tomcat-embed-jasper的依赖
在配置文件中加个两个配置
spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp


默认的资源的路径 为{classpath:/META-INF/resources/", "classpath:/resources/", 
"classpath:/static/", "classpath:/public/"};




springboot 拦截器
1.写一个拦截器,实现HandlerInterceptor接口
2.写一类,继承WebMvcConfigrerAdpter,重写 addInterceptoers方法,
并调用registry.addInterceptor把上一步的拦截器加进去。


springboot 配置Tomcat
1.实现 EmbeddedServertContainerCustomizer接口, 把实现类加入spring容器




事务: 使用@EnabelTransactionManagement启用事务  用@Transaction


日志:logging.level.*=  来设置日志级别
  logging.file 指定文件名字  默认10M 日志进行分割
  loggin.pattern.console  指定控制台输出格式
springboot默认支持logback, classpath下放一个logback.xml或者logback-spring.xml即可


springboot web环境加入 spring-boot-actuator依赖 开启监控




springboot 打包使用 appassmbler-maven-plugin
原创粉丝点击