简介
springboot最简单的理解时对spring的封装,它的两个最主要核心功能是起步依赖和自动化配置,可以简化spring的使用,避免一些样板代码和配置,但springboot又不仅仅如此,它还内置的servlet容器,这样就能够在不依赖于外部服务的情况下非常快速的构建一个单体应用,这位spring使用微服务架构奠定了坚实的基础。另外,起步依赖和自动化配置具有很好的扩展性,能够很方便的进行扩展,同时springboot还提供了生产级别的metrics和healthcheck机制。
创建springboot项目最简单的方式是通过start.spring.io来创建,另外idea也提供了springboot的Initializer,能够方便快捷的创建springboot项目。
springboot可以使用propertiess文件作为配置文件,也可以使用yml文件作为配置文件,但目前在项目中大多数情况下都是使用yml,因为更加的清晰简洁。
Actuator
actuator是springboot提供的一个插拔式的监控组件,通过暴露不同的endpoint来实现对服务的不同维度的监控,使用actuator首先要引入springboot起步依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然后配置actuator的访问上下文
management:
server:
port: 8888
servlet:
context-path: /zzzz
endpoints:
web:
baes-path: /base
然后就可以通过以下的url访问health和info两个endpoint了,springboot默认值开启这两个endpoint,可以通过配置开启全部或部分endpoint
http://localhost:8888/contextPath/baes/health
http://localhost:8888/contextPath/base/info
GET | /autoconfig | 提供一份自动配置报告,记录了哪些自动配置条件通过了,哪些没通过 |
GET | /configprops | 描述配置属性如何注入bean |
GET | /beans | 描述应用程序上文里的全部Bean,以及他们的关系 |
GET | /dump | 获取线程活动快照 |
GET | /env | 获取全部环境属性 |
GET | /env/{name} | 根据名称获取特定的环境属性值 |
GET | /health | 报告应用程序健康指标 |
GET | /info | 获取应用程序的定制信息 |
GET | /mappings | 描述全部URI路径,以及他们和控制器的映射关系 |
GET | /metrics | 报告应用程序的度量信息 |
GET | /meticss/name | 报告指定名称的应用程序度量值 |
GET | /shutdown | 关闭应用程序,要求endpoints.shutdown.enabled=true |
GET | /trace | 提供基于http请求跟踪信息 |
多环境配置
一般多环境配置的需求有两个,一个是不同的环境加载不同的配置文件,另一个是不同的环境激活不同的实现类。对于配置文件,springboot遵循application-{profile}.properties格式,一般的用法是,在项目中提供四个配置文件。
application.properties
application-dev.properties
application-test.properties
application-prod.properties
一般在application.properties中配置公共的配置信息,然后再不同的环境对应的配置文件中配置每个环境特有的配置,最后通过在application.properties文件中配置spring.profile.active=dev来激活不同的环境,一般都会配置成dev适用于本地开发环境,对于test或线上环境一般会通过。
java -jar xxx.jar --spring.profile.active=dev
来实现动态激活某个环境。
如果想要实现针对不同环境来激活不同的bean对象,那么可以适用@Profile("dev")注解来实现。
读取配置文件
读取主配置文件中的内容:@Value
读取自定义配置文件中的内容:
custom.properties
abc.stu.name=zhangsan
abc.stu.age=23
abc.stu.score=93
@Component
@PropertySSource(value = "classpath:custom.properties", encoding="utf-8"))
@ConfigurationProperties("abc.student")
@Data
public class Student{
private String name;
private String age;
private int age;
private double score;
}
读取List属性
def.c.cities[0]=beijing
def.c.cities[1]=shanghai
def.c.cities[2]=guangzhou
@Component
@PropertySSource(value = "classpath:custom.properties", encoding="utf-8"))
@ConfigurationProperties("abc.student")
@Data
public class Student{
private List<String> cities;
}
日志
在记录日志的时候绝大多数情况都是采用logback,因为它的性能更好,在spingboot中是使用logback首先要引入spring-boot-starter-logging包,然后进行配置。
配置的方式有两种,一种是在在application.properties中进行配置,但一般不这么用,一般是引入一个lockback.xml配置文件,以logback原生的方式进行配置。
缓存
- 在pom中添加redis与spring boot整合的依赖;
- 在配置文件中注册redis连接信息;
- 类实体实现序列化接口;
- 在启动类上添加@EnableCache接口;
- 在增删改方法上添加@CacheEvict,在查询方法上添加@Cacheable;
- 如果使用api的方式操作redis,则需要注入redis template,然后通过redis template获取到redis操作对象后就可以对redis进行操作了;
原理
spring boot简单理解就是对spring的封装,它主要提供了两个核心功能:起步依赖和自动配置,起步依赖的主要作用是托管依赖jar包,避免搭建项目是的繁琐的jar包管理,自动配置是对一些基础的配置自动化,避免开发过程中的一些繁琐的配置。所以理解spring boot原理,最基础的要从这两个方面入手。
起步依赖
对于起步依赖这主要是maven的知识,简单来说就是提供一个总的、包含了所有相关依赖的pom。
自动配置
自动配置的核心是spring 4中提供的“条件化的bean”,也就是 @ConditionalOnXxxx注解,能够根据不同的条件加载不同的bean对象到IoC容器中,比如@ConditionalOnBean是当容器中存在指定的bean对象
- @ConditionalOnMissingBean,当容器里不存在指定的bean对象;
- @ConditionalOnClass,当类路径下有指定类的class字节码文件;
- @ConditionalOnMissClass,当类路径下没有指定的class字节码文件;
- @ConditionalOnProperty(prefix="xx.xx", value="enable", matchIfMissing="true"),代表xx.xx为enable时值为true,默认也为true,xx.xx代表属性文件中的配置;
自动装配需要分析auto-config包,spring boot提供了一个spring-boot-autoconfigure.jar的包,这个包中包含了所有模块的自动化配置类。
这个文件中配置的是每个模块对应的AutoConfiguration,而这些Configuration类中就是一系列的@ConditionOnXxx注解表述的Bean
@Bean
@ConditionalOnClass(
name = {"org.springframework.security.authentication.event.AbstractAuthenticationEvent"}
)
@ConditionalOnMissingBean({AbstractAuthenticationAuditListener.class})
public AuthenticationAuditListener authenticationAuditListener() throws Exception {
return new AuthenticationAuditListener();
}
@Bean
@ConditionalOnClass(
name = {"org.springframework.security.access.event.AbstractAuthorizationEvent"}
)
@ConditionalOnMissingBean({AbstractAuthorizationAuditListener.class})
public AuthorizationAuditListener authorizationAuditListener() throws Exception {
return new AuthorizationAuditListener();
}
@SpringBootApplication是spring boot应用程序启动的入口,每个spring boot的启动类上都要标注这个注解。
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters =
{@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}),
@Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})}
)
public @interface SpringBootApplication {}
这里面比较重要的一个就是@EnableAutoConfiguration,这个注解表明开启自动装配功能,
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {}
在这个注解中引入了AutoConfigurationImportSelect,这个类时整个spring boot自动化装配的入口,简单的说,这个类就是从autoconfgure包中加载spring.factories,然后把配置的所有Configuration加载到IoC容器,再由IoC容器根据@ConditionanlOnXxx决定加载哪些Bean对象,这样就完成了环境的自动化配置。