中国建设网站的证件怎么查,安徽省芜湖建设定额网站,wordpress 移动主题 crystal,南宁网站开发一、基础
官方文档地址#xff1a;Spring Boot
注#xff1a;以下部分例子 有些用到 .properties 方式#xff0c;有些用 .yml方式#xff0c;两者可自行学习#xff0c;这里部分是为了省空间而写 .properties 方式。
1、泛谈
#xff08;1#xff09;优势
快速构建…一、基础
官方文档地址Spring Boot
注以下部分例子 有些用到 .properties 方式有些用 .yml方式两者可自行学习这里部分是为了省空间而写 .properties 方式。
1、泛谈
1优势
快速构建去除传统 spring xml配置内嵌web容器并提供监控/健康检查starters 自动依赖包大量自动配置“习惯大于配置”
2springboot项目创建
创建 maven项目引入 相应的 starters继承parent 引入web并加入 maven 插件创建 启动类SpringBootApplication SpringApplication.run()
注如果不想继承 parent也可以
2、属性配置
1获取方式
Value 方式 或者 Environment
// Value(${name:default}) 双引号后面可以加 默认值
Value(${name})
private String name;等同于下面不过下面这种如果配置文件中不存在值的时候启动不会报错而上面启动都不行
Autowired
private Environment env;
env.getProperty(name);
ConfigurationProperties 方式
// 其实基于 EnableConfigurationProperties 来完成
Component
ConfigurationProperties(prefix lin)
Data
public class LinProperties {private String name;private Integer age;private String email;private Boolean boss;private Date birth;private MapString, String system;
// private UserSystem system;private ListString work;
}
2数据类型
简单类型
lin:name: 小林boss: falsebirth: 1995/02/17email: 928232596qq.com
对象 Map类型
lin:system:username: linzhuzaipassword: 123456#也可以写成 system: {username: linzhuzai, password: 123456}#如果是 properties文件还能写成 lin.system[username]linzhuzai 或者 lin.system.usernamelinzhuzai# 如果获取方式是 Value(#{${lin.system}})
lin:system: {MY0003: MY0001}List Set类型
lin:work:- 佛山- 广州#也可以写成 work: [佛山,广州]#如果是 properties文件还能写成 lin.work[0]佛山
3引入文件
导入xml 文件
ImportResource(locations {classpath:beans.xml})
SpringBootApplication
public class SpringbootApplication {public static void main(String[] args){SpringApplication.run(SpringbootApplication.class, args);}
}
导入 properties 文件
PropertySource(value {classpath:lin.properties})
Component
ConfigurationProperties(prefix lin)
public class LinProperties {
}
4多环境配置
如有多个 yml文件配置application.yml application-dev.yml application-prod.yml
yml 文件spring.profiles.activedev启动命令行--spring.profiles.activedev虚拟机参数-Dspring.profiles.activedev
5随机值 引用值
随机值利用 random如 age: ${random.int}引用值可引用已配置的值如 name: ${lin.name}
6配置目录优先级
–file: ./config/–file: ./–classpath: /config/–classpath: /
7配置优先级
命令行参数配置系统环境变量bootstrap.properties/bootstrap.yml优先级高于 application它是 spring-cloud的jar包外内的 application-{profile}.properties / application-{profile}.ymljar包外内的 application.properties / application.yml
注① 命令行系统环境bootstrapapplication-{profile}application② propertiesyamlyml③ jar包外 优先于 包内
二、SpringBootApplication 1、SpringBootConfiguration等同于 Configuration
2、EnableAutoConfiguration
1
3、ComponentScan
三、原理
1、Import 原理
1Import可用来导入 普通类或者配置类这样类本身和类中配置的 Bean都可被 spring托管
ImportSelector ImportBeanDefinitionRegistrar
2、自动装配
1工作流程在 classpath下搜索所有 META-INF/spring.factories文件将里面 org.springframework.boot.autoconfigure.EnableAutoConfiguration 配置的项都加载到 spring容器
2源码
SpringApplication.run() prepareContext() load() createBeanDefinitionLoader()
AbstractApplicationContext.refresh() invokeBeanFactoryPostProcessors() invokeBeanDefinitionRegistryPostProcessors() ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry() ConfigurationClassParser.parse() processConfigurationClass() doProcessConfigurationClass()EnableAutoConfiguration Import({AutoConfigurationImportSelector.class}) AutoConfigurationImportSelector selectImports() getAutoConfigurationEntry()
getCandidateConfigurations() SpringFactoriesLoader.loadFactoryNames() META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfigurationXXXX
3实现关键
ImportSelectorConditional可限制加载条件如某些类存在或不存在才实例化若用户自己实现则不加载 springboot自动配置的等等EnableAutoConfiguration(exclude ) 可进行排除
3、启动原理
1源码
// 启动流程在启动类 SpringApplication run() 到 AbstractApplicationContext refresh() 之间做了很多准备工作用 listener监听器来实现比如自动装配、内嵌tomcat等等// 创建 ApplicationContext实例、注册 CommandLinePropertySource参数命令行解析器、刷新 applicationContext并加载单例对象、触发 CommandLineRunner的bean// EventPublishingRunListener 执行 ApplicationStartingEvent、ApplicationEnvironmentPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent
// 启动流程
SpringApplication.run() new SpringApplication() getSpringFactoriesInstances() SpringFactoriesLoader.loadFactoryNames() loadSpringFactories() 加载 spring-boot/spring-boot-autoconfigure下的META-INF/spring.factories SpringApplication.createSpringFactoriesInstances() SpringApplication.run() getRunListeners() new DefaultApplicationArguments() SpringApplication.prepareEnvironment()/configureEnvironment()/configurePropertySources() SpringApplication.createApplicationContext() SpringApplication.prepareContext() postProcessApplicationContext()SpringApplication.refreshContext()// spring-boot
org.springframework.context.ApplicationContextInitializer
org.springframework.context.ApplicationListener
org.springframework.boot.env.PropertySourceLoader
org.springframework.boot.SpringApplicationRunListener
org.springframework.boot.env.EnvironmentPostProcessor// spring-boot-autoconfigure
org.springframework.context.ApplicationContextInitializer
org.springframework.context.ApplicationListener
org.springframework.boot.autoconfigure.AutoConfigurationImportListener
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter
org.springframework.boot.autoconfigure.EnableAutoConfiguration 3、Tomcat 原理 四、JDBC Mybatis DAO
springboot整合数据库和mybatis_真是呆子啊的博客-CSDN博客
五、Web开发
1、静态资源
1路径优先级优先级高到低
classpath:/META-INF/resourcesclasspath:/resourcesclasspath:/staticclasspath:/public
2、模板引擎
3、视图配置
3、组件配置
2、自定义 web配置springboot集成mvc_spring boot 继承mvc_真是呆子啊的博客-CSDN博客
2、国际化
6、认证
六、应用
1、日志
springboot 默认是 slf4j logbackspring 默认是 jclmybatis 默认是 slf4j log4j
1日志包
日志抽象JCL(Jakarta Commons Logging)、slf4j日志实现logback、log4j、log4j2、jul(java.util.logging)、jboss-logging日志适配slf4j-log4j12、slf4j-jdk14
2日志配置
3日志级别
2、缓存
3、异常springboot 全局异常处理_真是呆子啊的博客-CSDN博客
4、定时任务springboot集成定时任务_真是呆子啊的博客-CSDN博客
5、事务springboot 事务与并发及回滚_真是呆子啊的博客-CSDN博客
6、异步Async EnableAsync
7、消息/事件监听ApplicationListener ApplicationEvent
七、运维
1、测试springboot 单元测试集合_真是呆子啊的博客-CSDN博客
2、部署
1开发热部署spring-boot-devtools
2linux 下部署springboot 项目在linux下部署_真是呆子啊的博客-CSDN博客
3docker 下部署docker基于Dockerfile将springboot项目构建成镜像并推送远端仓库_真是呆子啊的博客-CSDN博客
3、监控
添加依赖spring-boot-starter-actuator访问项目 项目访问 /actuator可配置监控参数 management.server.port8888
management.server.servlet.context-path/lin 八、扩展
1、自定义 starter 2、使用外置 tomcat
SpringBootServletInitiailzer 3、启动时执行方式
1监听 Bean
使用PostConstruct注解
实现InitializingBean和 DisposableBean接口等同于配置 init-method
2监听 spring 容器
接口 ApplicationContextInitializer
实现CommandLineRunner接口
实现ApplicationRunner接口 实现ServletContextAware接口并重写其setServletContext方法
实现ServletContextListener接口
WebApplicationInitializer
SpringBootServletInitializer
4、springboot2 的变化
基于 java8可以不用模板方法提供默认实现而是直接在接口上默认实现