spring-boot -starter-parent
作为spring boot快速构建spring工程所依赖包的模板 spring-boot-starter-parent的继承了spring-b boot-dependencies(2.1.8 Release) dependencies中定义了spring boot快速构建所需要的依赖包及版本信息 dependencies中有版本锁定dependencManagement版本锁定,定义了一个最优的版本搭配套装,如果一个工程继承了spring-booot-dependencies,那么该工程即可以不用配置依赖包版本
springboot启动流程
在SpringApplication中,springboot核心启动代码为new SpringApplication(primarySource).run(args)
,其中:
实例化一个SpringApplication是为了加载各种配置信息 实例化对象的.run
方法,是为了初始化容器
spring-boot-starter-web
spring-boot-starter-web是spring-boot-start-parent中已经定义好的坐标和版本信息 业务工程中,只需要指定spring-boot-starter-web的坐标即可,不需要再指定版本
artifactid
springboot自己提供的依赖,一般功能名称在最后,例如spring-boot-starter-jdbc 而第三方提供的依赖,一般功能名字在最前,例如mybatis-spring-boot-starter
spring配置文件
优先级顺序:application.properties>application.yml>application.yaml 如果在写yml过程中没有提示联想信息,需要引入dependencies-processor依赖
spring-boot-configuration-processor
address:
- beijing
- tianjin
address: [beijing,tianjin]
user:
name: zh
age: 18
phones:
- 15522222222
- 15551111111
@Value ( "${address[0]}" )
private String address;
@Autowired
private Environmen env;
public void print ( ) {
sout ( env. getProperty ( "address[0]" ) ) ;
}
@Component
@ConfigurationProperties ( prefix = "user" )
public class user ( ) {
private String name;
private Int age;
private String [ ] phones;
}
@Condition
按照条件来判断,满足条件的类会被容器注册为bean 基本用法:
public class ConditionCheck implements Condition ( ) {
@Override
public boolean matches ( CondtionContext context, AnnotatedTypeMetadta metadata) {
return false ;
}
}
@Configuration
public class CustomerizedConfig {
@Bean
@Condition ( ConditionCheck . class )
pubic Config config ( ) {
return new Config ( ) ;
}
}
@Target ( { ElementType . TYPE , ElementType . METHOD } )
@Retention ( RetentionPolicy . RUNTIME )
@Documented
@Condition ( ConditionCheck . class )
public @interface MyCondtion {
String value ( ) ;
}
@Configuration
public class CustomerizedConfig {
@Bean
@MyCondtion ( "abc" )
pubic Config config ( ) {
return new Config ( ) ;
}
}
Condition相关的还有很多注解:
@ConditionalOnClass // 判断环境中是否又对应的文件 @ConditionalOnProperty // 判断配置文件中是否有对应属性值 @ConditionalOnMissingBean // 判断环境中没有对应的bean matches方法两个参数:
CondtionContext context:上下文对象,可获取属性值,获取类加载器,获取BeanFactory等 AnnotatedTypeMetadta metadata:元数据对象,用于获取注解属性。
@Enable开头的注解
spring boot中的enbale开头的注解,用于动态启动某些功能,其内部都是实用@import导入配置类,进行bean的动态加载。 @ComponentScan是@SpringBootApplication中的注解,其作用是扫描该类同目录及其子目录下所有的bean Enable开头注解的由来,举例说明下演变过程:
@SpringBootApplication
@Component ( "com.test" )
public class AprojectAapplication {
xxx
context. getBean ( "test" ) ;
}
@SpringBootApplication
@Import ( test. class )
public class AprojectAapplication {
xxx
context. getBean ( "test" ) ;
}
@Target ( ElementType . TYPE )
@Retention ( RetentionPolicy . RUNTIME )
@Documented
@Import ( test. class )
public @interface EnableTest {
}
@SpringBootApplication
@EnableTest
public class AprojectAapplication {
xxx
context. getBean ( "test" ) ;
}
@Import
被@Import导入的类,会被加载到IOC容器中 @Import导入的四种方式:
@Configuration
public class Config {
@Bean
public Test test ( ) {
return new Test ( ) ;
}
@Bean
public Test2 test2 ( ) {
return new Test2 ( ) ;
}
}
* 导入ImportSelector实现类(常用于加载配置文件中的类)
public TestImportSelector implements ImportSelector {
@Override
public String [ ] selectImports ( AnnotationMetadata annotationMetadata ) {
return String [ ] { "com.Test" , "com.Test2" } ;
}
}
@SpringBootApplication
@Import ( TestImportSelector . class )
public class AprojectAapplication {
xxx
context. getBean ( "test" ) ;
}
* 导入ImportBeanDefinitionRegistrar实现类
public class MyImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions ( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder . rootBeanDefinition ( Test . class ) . getBeanDefinition ( ) ;
registry. registerBeanDefinition ( "test" , beanDefinition) ;
}
}
@EnableAutoConfiguration
@EnableAutoConfiguration注解内部使用@Import加载配置类 工程会根据依赖第三方包中的META/spring.factories配置,扫描配置类,当SpringBoot应用时,会自动初始化这些bean(关于spring.factories的意义,点击这篇文章 可以了解更多) 第三方的bean也是依赖conditon条件进行初始化的
模拟redis 自定义一个starter,构建一个第三方包
定义一个redis-speeder-autoconfig,用来配置和模拟链接redis 定义一个redis-speeder-starter,引用redis-spring-boot-autoconfigure,用来整合和包装 使用META-INF/spring.factories,控制容器是否加载jedis的bean,关于 测试starter,测试获取jedis的bean
package com. entity ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. stereotype. Component ;
@Component
@ConfigurationProperties ( prefix = "redis" )
@ConditionalOnMissingBean ( RedisConfig . class )
public class RedisConfig {
private String host = "localhost" ;
private int port = 6379 ;
public String getHost ( ) {
return host;
}
public void setHost ( String host) {
this . host = host;
}
public int getPort ( ) {
return port;
}
public void setPort ( int port) {
this . port = port;
}
}
package com. entity ;
public class RedisMock {
public RedisMock ( RedisConfig rc) {
System . out. println ( "redis initint..." ) ;
System . out. println ( "redis mocked host:" + rc. getHost ( ) ) ;
System . out. println ( "redis mocked port:" + rc. getPort ( ) ) ;
System . out. println ( "redis inited! " ) ;
}
public void print ( ) {
System . out. println ( "redis server is running" ) ;
}
}
package com. speeder ;
import com. entity. RedisConfig ;
import com. entity. RedisMock ;
import org. springframework. boot. context. properties. EnableConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
@EnableConfigurationProperties ( RedisConfig . class )
public class RedisAutoConfiguration {
@Bean
public RedisMock redisMock ( RedisConfig RedisConfig ) {
return new RedisMock ( RedisConfig ) ;
}
}
org. springframework. boot. autoconfigure. EnableAutoConfiguration= \
com. entity. RedisConfig
< dependency>
< groupId> org. example< / groupId>
< artifactId> redis- speeder- starter< / artifactId>
< version> 1.0 - SNAPSHOT < / version>
< / dependency>
package com. speeder ;
import com. entity. RedisMock ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
import org. springframework. context. ConfigurableApplicationContext ;
@SpringBootApplication
public class TestApplication {
public static void main ( String [ ] args) {
ConfigurableApplicationContext context = SpringApplication . run ( TestApplication . class , args) ;
RedisMock redisMock = ( RedisMock ) context. getBean ( "redisMock" ) ;
redisMock. print ( ) ;
System . out. println ( "bean = " + redisMock) ;
}
}
redis. host= 233.3 .3 .3
redis. port= 1111
2022 - 09 - 24 17 : 06 : 03.256 INFO 1588 -- - [ main] com. speeder. TestApplication : Starting TestApplication using Java 1.8 .0_231 on DESKTOP - E7BNK32 with PID 1588 ( E : \mycode\spring- study\spring- boot\redis- speeder- test\target\classes started by zhaoheng in E : \mycode\spring- study\spring- boot)
2022 - 09 - 24 17 : 06 : 03.256 INFO 1588 -- - [ main] com. speeder. TestApplication : No active profile set, falling back to 1 default profile: "default"
2022 - 09 - 24 17 : 06 : 04.257 INFO 1588 -- - [ main] o. s. b. w. embedded. tomcat. TomcatWebServer : Tomcat initialized with port ( s) : 8080 ( http)
2022 - 09 - 24 17 : 06 : 04.272 INFO 1588 -- - [ main] o. apache. catalina. core. StandardService : Starting service [ Tomcat ]
2022 - 09 - 24 17 : 06 : 04.272 INFO 1588 -- - [ main] org. apache. catalina. core. StandardEngine : Starting Servlet engine: [ Apache Tomcat / 9.0 .65 ]
2022 - 09 - 24 17 : 06 : 04.397 INFO 1588 -- - [ main] o. a. c. c. C. [ Tomcat ] . [ localhost] . [ / ] : Initializing Spring embedded WebApplicationContext
2022 - 09 - 24 17 : 06 : 04.397 INFO 1588 -- - [ main] w. s. c. ServletWebServerApplicationContext : Root WebApplicationContext : initialization completed in 1094 ms
redis initint. . .
redis mocked host: 233.3 .3 .3
redis mocked port: 1111
redis inited!
2022 - 09 - 24 17 : 06 : 04.678 INFO 1588 -- - [ main] o. s. b. w. embedded. tomcat. TomcatWebServer : Tomcat started on port ( s) : 8080 ( http) with context path ''
2022 - 09 - 24 17 : 06 : 04.694 INFO 1588 -- - [ main] com. speeder. TestApplication : Started TestApplication in 1.928 seconds ( JVM running for 3.405 )
redis server is running
bean = com. entity. RedisMock@70325d20
SpringBoot Actuator监控
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- actuator< / artifactId>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
访问监控内的相关资源:
访问监控台:localhost:8080/actuator,会返回控制台所有地址导航信息 访问配置信息:localhost:8080/actuator/info等 健康检查:localhost:8080/actuator/health,可以查看项目是否在正常运行,也可以检查其他第三方依赖的健康状况,例如redis、数据库等 开启actuator所有监控:application.properties中加入management.endpoints.web.exposure.include=*
查看ioc容器中所有的bean:localhost:8080/actuator/bean 查看项目的环境配置信息:localhost:8080/actuator/env 查看所有的接口请求路径:localhost:8080/actuator/mappings
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/actuator" ,
"templated" : false
} ,
"health-component" : {
"href" : "http://localhost:8080/actuator/health/{component}" ,
"templated" : true
} ,
"health-component-instance" : {
"href" : "http://localhost:8080/actuator/health/{component}/{instance}" ,
"templated" : true
} ,
"health" : {
"href" : "http://localhost:8080/actuator/health" ,
"templated" : false
} ,
"info" : {
"href" : "http://localhost:8080/actuator/info" ,
"templated" : false
}
}
}