图文并茂,Spring Boot Starter 万字详解!还有谁不会?
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
- 一、SpringBoot的starter简介 -
1.1 什么是starter(场景启动器)
- 二、SpringBoot场景启动器的原理 -
2.1 自动配置原理
2.1.1 自动配置类的获取与注入
@SpringBootApplication //标注这个类是一个springboot的应用
public class CommunityApplication {
public static void main(String[] args) {
//将springboot应用启动
SpringApplication.run(CommunityApplication.class, args);
}
}
2.1.2 自动配置的过程
//表示这是一个配置类,和以前编写的配置文件一样,也可以给容器中添加组件;
@Configuration
//将与配置文件绑定好的某个类注入到容器中,使其生效
//进入这个HttpProperties查看,将配置文件中对应的值和HttpProperties绑定起来;
//并把HttpProperties加入到ioc容器中
@EnableConfigurationProperties(HttpProperties.class)
//Spring底层@Conditional注解
//根据不同的条件判断,如果满足指定的条件,整个配置类里面的配置就会生效;
//这里的意思就是判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
//判断系统中有没有CharacterEncodingFilter这个类,如果有配置类才生效
@ConditionalOnClass(CharacterEncodingFilter.class)
//判断配置文件中是否存在某个配置:spring.http.encoding.enabled;
//matchIfMissing = true表明即使我们配置文件中不配置pring.http.encoding.enabled=true,该配置类也是默认生效的;
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
//该类已经与配置文件绑定了
private final HttpProperties.Encoding properties;
//构建该自动配置类时将与配置文件绑定的配置类作为入参传递进去
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name()); //注入bean时使用配置类中属性的值进行初始化,相当于将配置文件中的值映射到了组件的某些属性上
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter; //注入配置好的bean
}
}
//从配置文件中获取指定的值和bean的属性进行绑定
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {
// .....
}
2.2 SpringBoot自动配置使用总结
@Conditional扩展注解 | 作用(判断是否满足当前条件) |
---|---|
@ConditionalOnNotWebApplication | 当前不是web环境 |
@ConditionalOnWebApplication | 当前是web环境 |
@ConditionalOnResource | 类路径下是否有指定资源文件 |
@ConditionalOnExpression | 满足SpEL表达式指定 |
@ConditionalOnMissingBean | 容器中不存在指定的Bean |
@ConditionalOnBean | 容器中是否存在指定的Bean |
@ConditionalOnSingleCandidate | 容器中只有一个指定的Bean,或者这个Bean是首选Bean |
@ConditionalOnJava | 系统的java版本是否符合要求 |
@ConditionalOnMissingClass | 系统中没有指定的类 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
#在配置文件中开启springboot的调试类
debug=true
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition)
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.CglibAutoProxyConfiguration matched:
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
AuditAutoConfiguration#auditListener matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; SearchStrategy: all) did not find any beans (OnBeanCondition)
AuditAutoConfiguration#authenticationAuditListener matched:
- @ConditionalOnClass found required class 'org.springframework.security.authentication.event.AbstractAuthenticationEvent' (OnClassCondition)
- @ConditionalOnMissingBean (types: org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener; SearchStrategy: all) did not find any beans (OnBeanCondition)
Negative matches:
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
AopAutoConfiguration.JdkDynamicAutoProxyConfiguration:
Did not match:
- @ConditionalOnProperty (spring.aop.proxy-target-class=false) did not find property 'proxy-target-class' (OnPropertyCondition)
AppOpticsMetricsExportAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'io.micrometer.appoptics.AppOpticsMeterRegistry' (OnClassCondition)
ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
Exclusions:
-----------
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Unconditional classes:
----------------------
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration
org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration
- 三、自定义场景启动器 -
3.1 starter的命名规范
3.2 starter模块整体结构
3.3 autoconfigure模块开发
3.3.1 依赖引入
org.springframework.boot
spring-boot-autoconfigure //包含很多与自动配置相关的注解的定义,必须要引入
org.springframework.boot
spring-boot-configuration-processor //非必须的,引入后可以在配置文件中输入我们自定义配置的时候有相应的提示,也可以通过其他.properties文件为相关类进行属性映射(SpringBoot默认使用application.yml)
true
3.3.2 xxxAutoConfiguration的实现
@Configuration
@ConditionalOnxxx
@ConditionalOnxxx//限定自动配置类生效的一些条件
@EnableConfigurationProperties(xxxProperties.class)
public class xxxAutoConfiguration {
@Autowired
private xxxProperties properties;
@Bean
public static BeanYouNeed beanYouNeed() {
BeanYouNeed bean = new BeanYouNeed()
bean.setField(properties.get(field));
bean.setField(properties.get(field));
bean.setField(properties.get(field));
......
}
}
3.3.3 xxxProperties的实现
@ConfigurationProperties(prefix = "your properties") //使用@ConfigurationProperties注解绑定配置文件
public class xxxProperties {
private boolean enabled = true;
private String clientId;
private String beanName;
private String scanBasePackage;
private String path;
private String token;
}
3.3.4 配置spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.meituan.xframe.boot.mcc.autoconfigure.xxxAutoConfiguration
3.4 Starter模块开发
================================================================
添加了对autoconfigure模块的引用
com.test.starter
xxx-spring-boot-autoconfigure
===============================================================
其他的一些必要依赖项
commons-collections
commons-collections
作者 | 威尼斯的泪H
来源 | csdn.net/qq_21310939/article/details/107401400
加锋哥微信: java1239 围观锋哥朋友圈,每天推送Java干货!
评论